Android手机蓝牙连接到蓝牙模块

时间:2014-05-16 19:11:13

标签: android bluetooth

我希望将我的应用程序连接到带有蓝牙模块的传感器。我将手机蓝牙配对模块蓝牙。然后通过编程我创建一个模块连接。但我在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();
    }

1 个答案:

答案 0 :(得分:0)

可能因为你的MyDevice错误而无法连接。它必须是远程设备而不是您的设备,您可以使用哪个来监听用户监听服务记录。你必须创建

           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();

     }

它只是它应该看起来你应该在inteernet中搜索更多内容的一部分