Android Bluetooth COM port

前端 未结 1 1680
予麋鹿
予麋鹿 2021-02-01 11:28

I have spent some time researching Android\'s ability to communicate with bluetooth devices that are designed to communicate over a Bluetooth COM port on a PC. I haven\'t been

相关标签:
1条回答
  • 2021-02-01 12:05

    Yes, Android can connect to Bluetooth COM ports on PC's. I am currently developing such an application. Here is a code example (Ite requires the bluetooth permissions te be set in the Manifest.xml file):

    <uses-permission android:name="android.permission.BLUETOOTH" />
    

    Java:

    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if (adapter == null) {
        // Device does not support Bluetooth
        finish(); //exit
    }
    
    if (!adapter.isEnabled()) {
    //make sure the device's bluetooth is enabled
        Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBluetooth, REQUEST_ENABLE_BT);
    }
    
    final UUID SERIAL_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //UUID for serial connection
    mac = "00:15:83:3D:0A:57"; //my laptop's mac adress
    device = adapter.getRemoteDevice(mac); //get remote device by mac, we assume these two devices are already paired
    
    
     // Get a BluetoothSocket to connect with the given BluetoothDevice
    BluetoothSocket socket = null;
    OutputStream out = null;
    try {
        socket = device.createRfcommSocketToServiceRecord(SERIAL_UUID); 
    } catch (IOException e) {}
    
    try {           
        socket.connect(); 
        out = socket.getOutputStream();
        //now you can use out to send output via out.write
    } catch (IOException e) {}
    
    0 讨论(0)
提交回复
热议问题