BluetoothChat核心类BluetoothChatService,该类用于管理与其他设备的蓝牙连接和设置。该类包含AcceptThread、ConnectedThread、ConnectThread三个线程。AcceptThread用于监听传入的连接。ConnectedThread用于管理与远程设备的连接,处理所有数据的传入与传出。ConnectThread用于连接远程设备。
类图如下:
1 //获取设备蓝牙 2 BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 3 //判断蓝牙是否可用,不可以则跳转到系统蓝牙设置界面 4 if (!mBluetoothAdapter.isEnabled()) { 5 Intent enableIntent = new Intent( 6 BluetoothAdapter.ACTION_REQUEST_ENABLE); 7 startActivityForResult(enableIntent, REQUEST_ENABLE_BT); 8 }
1 //设置设备蓝牙可以被其他设备搜索 2 //SCAN_MODE_CONNECTABLE_DISCOVERABLE 表明该蓝牙设备同时可以扫码其他蓝牙设备,并且可以被其他蓝牙设备扫描到。 3 4 private void ensureDiscoverable() { 5 6 if (mBluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) { 7 Intent discoverableIntent = new Intent( 8 BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); 9 discoverableIntent.putExtra( 10 BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); 11 startActivity(discoverableIntent); 12 } 13 }
1 //获取远程蓝牙设备 2 3 BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
1 //使用listenUsingRfcommWithServiceRecord和listenUsingInsecureRfcommWithServiceRecord以服务端的方式创建监听 2 3 public AcceptThread(boolean secure) { 4 BluetoothServerSocket tmp = null; 5 mSocketType = secure ? "Secure" : "Insecure"; 6 7 // Create a new listening server socket 8 try { 9 if (secure) { 10 tmp = mAdapter.listenUsingRfcommWithServiceRecord( 11 NAME_SECURE, MY_UUID_SECURE); 12 } else { 13 tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord( 14 NAME_INSECURE, MY_UUID_INSECURE); 15 } 16 } catch (IOException e) { 17 Log.e(TAG, "Socket Type: " + mSocketType + "listen() failed", e); 18 } 19 mmServerSocket = tmp; 20 } 21 接受socket连接 22 23 BluetoothSocket socket = null; 24 socket = mmServerSocket.accept();
1 //使用createRfcommSocketToServiceRecord和createInsecureRfcommSocketToServiceRecord以客户端的方式创建监听 2 3 public ConnectThread(BluetoothDevice device, boolean secure) { 4 mmDevice = device; 5 BluetoothSocket tmp = null; 6 mSocketType = secure ? "Secure" : "Insecure"; 7 8 // Get a BluetoothSocket for a connection with the 9 // given BluetoothDevice 10 try { 11 if (secure) { 12 tmp = device 13 .createRfcommSocketToServiceRecord(MY_UUID_SECURE); 14 } else { 15 tmp = device 16 .createInsecureRfcommSocketToServiceRecord(MY_UUID_INSECURE); 17 } 18 } catch (IOException e) { 19 Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e); 20 } 21 mmSocket = tmp; 22 } 23 24 //连接到服务端socket 25 mmSocket.connect();
1 //从socket中使用getInputStream和getOutputStream获取输入流和输出流 2 3 public ConnectedThread(BluetoothSocket socket, String socketType) { 4 Log.d(TAG, "create ConnectedThread: " + socketType); 5 mmSocket = socket; 6 InputStream tmpIn = null; 7 OutputStream tmpOut = null; 8 9 // Get the BluetoothSocket input and output streams 10 try { 11 tmpIn = socket.getInputStream(); 12 tmpOut = socket.getOutputStream(); 13 } catch (IOException e) { 14 Log.e(TAG, "temp sockets not created", e); 15 } 16 17 mmInStream = tmpIn; 18 mmOutStream = tmpOut; 19 } 20 21 //使用输入流读取数据 22 bytes = mmInStream.read(buffer); 23 24 //使用输出流写入数据 25 mmOutStream.write(buffer);
时间: 2024-10-12 20:53:06