蓝牙客户端Socket的与Sokcet流程是一样的。如下:
1、通过BluetoothDevice 的 createRfcommSockettoServerRecord(UUID),创建客户端蓝牙BluetoothSokcet
2、BluetoothSokcet 的 connect 方法,创建连接
3、通过 获取BluetoothSokcet 的 inputStream 、outputStream 读写数据
4、Socket 关闭。
服务端socket:
1、通过BluetoothAdapter 的 listenUsingRfcommWithServerSocket(name,UUID),创建服务端蓝牙BluetoothServerSocket
2、通过调用 BluetoothServerSocket 的 accept(),如果有客户端连接,会创建一个新的 BluetoothSocket,体现出并发性,可以同时与多个socket通讯。
3、通过 获取BluetoothSokcet 的 inputStream 、outputStream 读写数据
4、关闭
常见蓝牙调用方法:
1、BluetoothAdapter.getDefaultAdapter();
2、bluetoothAdapter.enable();
3、bluetoothAdapter.startDiscovery();(bluetoothAdapter.cancelDiscovery();)
4、广播:BluetoothDevice.ACTION_FOUND、BluetoothAdapter.ACTION_DISCOVERY_FINISHED
5、Set<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices();
6、BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
蓝牙客户端通过BluetoothDevice创建BluetoothSocket并建立连接,使用inputStream和outputStream进行数据传输。服务端利用BluetoothAdapter监听连接,accept()方法处理并发连接。文章还提及了蓝牙设备发现和绑定的相关方法。
1333

被折叠的 条评论
为什么被折叠?



