该篇只讲解到经典蓝牙的连接和通信。
注意相关传限要加好。
从ui界面开始设计逻辑代码;
ui界面以及其VIew的相关属性已贴出;
打开蓝牙;
1;先判断该设备是否支持蓝牙
2;打开蓝牙;
关闭蓝牙
1;关闭蓝牙;
搜索蓝牙;
1;利用广播
注意;
1;在这之前,我们需要startDiscovery()方法是一个异步方法,它会对其他蓝牙设备进行搜索,持续时间为12秒
public void findBlue(View v) {
setProgressBarIndeterminateVisibility(true);
setTitle("正在扫描....");
// 如果正在搜索,就先取消搜索
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
// 开始搜索蓝牙设备,搜索到的蓝牙设备通过广播返回
mBluetoothAdapter.startDiscovery();
}
2;广播需要注册文件
// 注册用以接收到已搜索到的蓝牙设备的receiver
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter);
3;在activity结束时要注销;
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
//解除注册
unregisterReceiver(mReceiver);
}
listview;
获取已匹配设备;
获取可连接设备
对两个listview进行操作;
对可连接设备进行匹配
进行连接;
方案一;利用反射进行客服端连接(成功)
public class ConnectThread extends Thread {
private final BluetoothDevice mmdevice;
private final BluetoothSocket mmSocket;
private Boolean isConnect = false;
public ConnectThread(BluetoothDevice device){
BluetoothSocket tmp = null;
//赋值给设备
mmdevice = device;
try {
Method m = mBluetoothDevice.getClass().getMethod(
"createRfcommSocket", new Class[] { int.class });
temp = (BluetoothSocket) m.invoke(mBluetoothDevice, 1);//这里端口为1
} catch(IOException e) {}
//赋值给BluetoothSocket
mmSocket = tmp;
}
public void run(){
try {
System.out.println("连接ing.....");
mmSocket.connect();
isConnect = true;
System.out.println("连接成功");
} catch (IOException e) {
try {
mmSocket.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return ;
}
}
//在线程中管理连接;
}
public void cancel(){
try {
mmSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
方案二使用uuid;客服端进行连接,服务端接收连接
//<客服端》
public class ConnectThread extends Thread {
private final BluetoothDevice mmdevice;
private final BluetoothSocket mmSocket;
public final String s = "00001101-0000-1000-8000-00805F9B34FB";
private Boolean isConnect = false;
public ConnectThread(BluetoothDevice device){
BluetoothSocket tmp = null;
//赋值给设备
mmdevice = device;
try {
//根据UUID创建并返回一个BluetoothSocket
tmp = device.createRfcommSocketToServiceRecord(UUID.fromString(s));
} catch(IOException e) {}
//赋值给BluetoothSocket
mmSocket = tmp;
}
public void run(){
try {
System.out.println("连接ing.....");
mmSocket.connect();
isConnect = true;
System.out.println("连接成功");
} catch (IOException e) {
try {
mmSocket.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return ;
}
}
//在线程中管理连接;
}
public void cancel(){
try {
mmSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//服务端
private final BluetoothServerSocket mmserverSocket;
public AcceptThread(){
BluetoothServerSocket tmp = null;
try {
tmp = BluetoothAdapter.getDefaultAdapter().listenUsingRfcommWithServiceRecord("蓝牙连接", UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mmserverSocket = tmp;
}
public void run(){
BluetoothSocket Socket = null;
while(true){
try {
Socket = mmserverSocket.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
break;
}
if(Socket == null){
try {
//manageConnectedSocket(Socket);
mmserverSocket.close();
break;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void cancel(){
try {
mmserverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}