【问题标题】:Android phone bluetooth connection to a bluetooth moduleAndroid手机蓝牙连接到蓝牙模块
【发布时间】:2014-05-16 19:11:12
【问题描述】:

我想将我的应用程序连接到带有蓝牙模块的传感器。我将手机蓝牙与模块蓝牙配对。然后通过编程我创建与模块的连接。但我在 Socket.connect() 中遇到问题,并且没有运行 Socket.connect() 行并显示 toast ("c")

UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard SerialPortService ID
    try {
        mSocket = MyDevice.createRfcommSocketToServiceRecord(uuid);
    } catch (IOException e) {
        Toast.makeText(this, "S", Toast.LENGTH_SHORT).show();
    }  

    ba.cancelDiscovery();

    try {
        mSocket.connect();
    } catch (IOException e){
        Toast.makeText(this, "C", Toast.LENGTH_SHORT).show();
    }

    try {
        mOutputStream = mSocket.getOutputStream();
        mInputStream = mSocket.getInputStream();
    } catch (IOException e) {
        Toast.makeText(this, "io", Toast.LENGTH_SHORT).show();
    }

【问题讨论】:

    标签: android bluetooth


    【解决方案1】:

    可能无法连接,因为您的 MyDevice 错误。它必须是远程设备,而不是您用来监听UsingRfcommWithServiceRecord 的设备。你必须创造

               BluetoothDevice  device = bluetoothadatper.getRemoteDevice();
    
    
    public class AcceptThread10 extends Thread {
        private  final UUID MY_UUID = UUID.fromString("0125bb20-d629-11e3-9c1a-0800200c9a66");
    
          @Override
          public void run() {
    
           try {
               BluetoothServerSocket bluesocketserver = bluetoothAdapter.listenUsingRfcommWithServiceRecord(MY_UUID); 
    
    
               while (true) {
    
    
                     socket = bluesocketserver.accept();
    
                     socket12.close();
    
         }
    

    它只是外观的一部分,您应该在互联网上搜索更多内容

    【讨论】:

    • 我认为 MyDevice 没有错,因为我使用 add MyDevice.getName() 测试我的代码并显示名称远程设备
    • 你在 logCat 中有什么?
    • 远程蓝牙设备的名称。
    • Meaby 无法连接,因为您的 BluetoothServerSocket 坏了?检查它是否在监听并且 UUID 必须相同
    • 远程设备是模块蓝牙HC-05,我不能编程BluetoothServerSocket。我在配对两个设备的手机中使用蓝牙设置。现在我想从模块蓝牙获取数据