连接到可在棉花糖设备上运行但无法在Oreo设备上运行的蓝牙设备,为什么?

时间:2019-01-29 13:34:34

标签: java android bluetooth

我正在制作一个与特定Arduino HC06蓝牙模块通信的应用程序。

我一直在使用棉花糖在三星S5 Neo上测试此应用程序,现在大部分工作完成后,我想在运行Android Oreo的主手机小米Redmi 5上试用一下。但这是行不通的。我的Oreo手机与HC06模块之间建立了一个成功的连接,除此之外它无法连接。

到目前为止我做了什么:

我确保我的Oreo电话和HC06模块已配对。 我通过运行时添加了一些额外的权限。到目前为止,我具有以下权限:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

我不知道为什么这可以在我拥有的旧手机上运行,​​而无法在新手机上运行。希望有人能指导我正确的方向,谢谢。

这是我用来查找蓝牙并连接到蓝牙的方法

void findBT() {
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if(mBluetoothAdapter == null) {
        Toast errore = makeText(MachineReady.this, "Error, enable bluetooth", Toast.LENGTH_LONG);
        errore.show();
    }

    if(!mBluetoothAdapter.isEnabled()) {
        Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBluetooth, 0);
    }

    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
    if(pairedDevices.size() > 0) {
        for(BluetoothDevice device : pairedDevices) {
            mmDevice = device;
            break;
        }
    }
    Log.e("Bluetooth", "Bluetooth device found01");
    //Toast found = makeText(MachineReady.this, "Bluetooth device found", Toast.LENGTH_LONG);
    //found.show();
}

void openBT() throws IOException {
    UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard SerialPortService ID
    mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
    mmSocket.connect();
    mmOutputStream = mmSocket.getOutputStream();
    mmInputStream = mmSocket.getInputStream();
    if(mmSocket.isConnected()){
        beginListenForData();
        bluConnection = true;
        //Toast opened = makeText(MachineReady.this, "Bluetooth is opened with" + mmDevice + mmSocket, Toast.LENGTH_LONG);
        //opened.show();
        Log.e("Bluetooth", "Bluetooth connection has been established4");

        try {
            msg = "1";
            mmOutputStream.write(msg.getBytes());
            //Toast b = Toast.makeText(this, msg, Toast.LENGTH_LONG);
            //b.show();
        } catch (IOException e) {
            Log.e("Send bluetooth", "Couldn't send text");
        }

    }else if(!mmSocket.isConnected()){
        Toast notOpened = makeText(MachineReady.this, "Bluetooth couldn't connect to the device", Toast.LENGTH_LONG);
        notOpened.show();
    }

在日志中,我收到一条异常消息,说:“无法找到或连接到蓝牙”

try {
            findBT();
            openBT();
        } catch (IOException e) {
            Log.e("Bluetooth", "Couldn't find or connect to bluetooth");
        }

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。

如ecle所述,我查找或搜索蓝牙单元的方法无法正常工作。

我改用这个:

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter
            .getBondedDevices();
    if (pairedDevices.isEmpty()) {
        Log.e("bluetooth",
                "No devices paired...");
        return ;
    }

    String MY_MAC_ADDR = getResources().getString(R.string.Bluetooth_MAC);

    for (BluetoothDevice device : pairedDevices) {
        Log.d("Bluetooth", "Device : address : " + device.getAddress() + " name :"
                + device.getName());
        if (MY_MAC_ADDR.equals(device.getAddress())) {
            mmDevice = device;
            break;
        }
    }

我现在正在使用MAC地址寻找特定的蓝牙设备

现在它可以完美运行了。 谢谢ecle指出这一点。