客户端
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private UUID uuid = UUID.fromString("00001106-0000-1000-8000-00805F9B34FB");
private String[] premissons = new String[]{
Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
};
public static final int REQUEST_CODE = 101;
public static final int OPEN_CODE = 102;
private List<BluetoothDevice> list_bond = new ArrayList<>();
private List<BluetoothDevice> list_dis = new ArrayList<>();
private MyAdapter adapter_bond;
private MyAdapter adapter_dic;
private BluetoothManager bluetoothManager;
private BluetoothAdapter bluetoothAdapter;
private MyRecevice myRecevice;
private Button bt_open;
private Button bt_close;
private Button bt_discovery;
private Button bt_bond;
private ListView listView_discovery;
private ListView listView_bond;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermissions(premissons,REQUEST_CODE);
initView();
bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter();
myRecevice = new MyRecevice();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
registerReceiver(myRecevice,intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(myRecevice);
}
private void initView() {
bt_open = (Button) findViewById(R.id.bt_open);
bt_close = (Button) findViewById(R.id.bt_close);
bt_discovery = (Button) findViewById(R.id.bt_discovery);
bt_bond = (Button) findViewById(R.id.bt_bond);
listView_discovery = (ListView) findViewById(R.id.listView_discovery);
listView_bond = (ListView) findViewById(R.id.listView_bond);
bt_open.setOnClickListener(this);
bt_close.setOnClickListener(this);
bt_discovery.setOnClickListener(this);
bt_bond.setOnClickListener(this);
adapter_dic = new MyAdapter(list_dis,this);
listView_discovery.setAdapter(adapter_dic);
adapter_bond = new MyAdapter(list_bond,this);
listView_bond.setAdapter(adapter_bond);
listView_discovery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
BluetoothDevice device = list_dis.get(i);
device.createBond();
}
});
listView_bond.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
BluetoothDevice device = list_bond.get(i);
try {
BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(uuid);
socket.connect();
if (socket.isConnected()){
socket.getOutputStream().write("大金牙正在和你配对".getBytes());
}else {
Toast.makeText(MainActivity.this, "连接失败", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
Toast.makeText(MainActivity.this, "连接失败", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_open:
Intent intent = new Intent();
intent.setAction(BluetoothAdapter.ACTION_REQUEST_ENABLE);
intent.setAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,120);
startActivityForResult(intent,OPEN_CODE);
break;
case R.id.bt_close:
bluetoothAdapter.disable();
break;
case R.id.bt_discovery:
list_dis.clear();
bluetoothAdapter.startDiscovery();
break;
case R.id.bt_bond:
list_bond.clear();
Set<BluetoothDevice> bondedDevices = bluetoothAdapter.getBondedDevices();
list_bond.addAll(bondedDevices);
adapter_bond.notifyDataSetChanged();
break;
}
}
class MyRecevice extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(BluetoothDevice.ACTION_FOUND)){
BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
list_dis.add(bluetoothDevice);
adapter_dic.notifyDataSetChanged();
}
}
}
}
服务端
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private UUID uuid = UUID.fromString("00001106-0000-1000-8000-00805F9B34FB");
private Button button;
private ImageView imageView;
private BluetoothManager manager;
private BluetoothAdapter adapter;
private String [] premissons = new String[]{
Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
};
public static final int REQUEST_CODE = 101;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermissions(premissons,REQUEST_CODE);
manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
adapter = manager.getAdapter();
initView();
}
private void initView() {
button = (Button) findViewById(R.id.button);
imageView = (ImageView) findViewById(R.id.imageView);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
try {
BluetoothServerSocket serverSocket = adapter.listenUsingInsecureRfcommWithServiceRecord(adapter.getName(), uuid);
BluetoothSocket socket = serverSocket.accept();
Toast.makeText(this, socket.getRemoteDevice().getName()+"连接上了", Toast.LENGTH_SHORT).show();
InputStream is = socket.getInputStream();
byte b[] = new byte[1024];
int read = is.read(b);
String string = new String(b,0,read);
Toast.makeText(this, ""+string, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}