参考:http://wiki.eoe.cn/page/Bluetooth.html
总体步骤:
要用到的类:
// 蓝牙设备
private BluetoothDevice bluetoothDevice;
// 蓝牙适配器
private BluetoothAdapter bluetoothAdapter;
// 客户端Socket
private BluetoothServerSocket bluetoothServerSocket;
// 服务器Socket
private BluetoothSocket bluetoothSocket;
1.注册广播 用于接收蓝牙搜索到的设备
2.打开蓝牙
3.搜索设备
4.通过BluetoothAdapter创建一个服务端,用于其他设备请求Socket
5.作为客户端请求服务端
至此链接成功
具体代码:
/**
* 打开蓝牙
*/
private void openBluetooth(){
state.setText("正在打开设备...");
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter != null && !bluetoothAdapter.isEnabled()) {
bluetoothAdapter.enable();
//设置蓝牙可见时间为3600秒
Intent enable = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
enable.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 3600);
startActivity(enable);
}
}
/**
* 开始搜索
*/
private void startFound(){
state.setText("搜索设备...");
new Thread(new Runnable() {
@Override
public void run() {
bluetoothAdapter.startDiscovery();
handler.sendEmptyMessage(0);
}
}).start();
}
/**
* 停止搜索
*/
private void stopFound(){
bluetoothAdapter.cancelDiscovery();
mList.clear();
adapter.notifyDataSetChanged();
state.setText("停止搜索");
}
/**
* 创建链接
*/
private void buildSocket(){
state.setText("正在创建链接....");
new Thread(new Runnable() {
@Override
public void run() {
try {
bluetoothServerSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord("aaa", uuid);
bluetoothSocket = bluetoothServerSocket.accept();
bluetoothServerSocket.close();
handler.sendEmptyMessage(1);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
/**
* 请求链接
*
*/
private void requestSocket(){
if(bluetoothDevice == null)
return;
new Thread(new Runnable() {
@Override
public void run() {
try {
bluetoothSocket = bluetoothDevice.createRfcommSocketToServiceRecord(uuid);
bluetoothSocket.connect();
bluetoothSocket.close();
handler.sendEmptyMessage(1);
} catch (IOException e) {
try {
bluetoothSocket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
}
}).start();
}
// 注册广播,接收蓝牙搜索到的设备
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
receiver = new MyBroadcast() {
@Override
public void foundDevice(BluetoothDevice device) {
mList.add(device);
adapter.notifyDataSetChanged();
}
};
registerReceiver(receiver, filter);
注册广播这里广播是这么定义的:
public abstract class MyBroadcast extends BroadcastReceiver{
@Override
public void onReceive(Context arg0, Intent arg1) {
if(arg1.getAction().equals(BluetoothDevice.ACTION_FOUND)){
BluetoothDevice device = arg1.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
foundDevice(device);
}
}
public abstract void foundDevice(BluetoothDevice device);
}
这里具体说明一下建立连接部分:建立连接的流程是:
服务端:怎么知道有人想要连接你?需要通过BluetoothAdapter启动一个请求监听BluetoothServerSocket,他有一个阻塞方法,当客户端通过BluetoothSocket来向你发送请求的时候,这个阻塞方法返回一个BluetoothSocket,这个Socket就是客户端的Socket了。我们可以通过这个socket向客户端进行I/O操作。这时BluetoothServerSocket可以关闭掉。
客户端:当通过BluetoothDevice调用createRfcommSocketToServiceRecord(UUID)获取一个BluetoothSocket对象后。就可以获取到服务器的Socket,UUID必须匹配服务器设备在打开BluetoothServerSocket 时用到的UUID,
注意:要确保在调用connect()时没有同时做设备查找,如果在查找设备,该连接尝试会显著的变慢,慢得类似失败了。