【问题标题】:Bluetooth not connecting on 4.4.2蓝牙未连接到 4.4.2
【发布时间】:2014-10-28 09:11:36
【问题描述】:

我的蓝牙设备已连接到我在 4.4.2 之前尝试过的所有 Android 版本。现在,它没有连接到 Galaxy Tab 4 或 S3。 Tab 3 与 4.1.2 连接良好。在尝试初始化BluetoothSocket 时,问题似乎出现在AcceptThread 中。我使用的代码基于 sdk 中的聊天示例。

我的接受码

private class AcceptThread extends Thread {
    // The local server socket
    private BluetoothServerSocket mmServerSocket;
    public boolean successInit = false;

    public AcceptThread() {
        closeAllConnections();          
        BluetoothServerSocket tmp = null;

        // Create a new listening server socket
        while(!successInit) {
            try {
                tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
                successInit= true;
                Log.i("TAG", "Socket Server Created");
            }
            catch(Exception e) {
                Log.i("TAG", e.getMessage());
                successInit = false;
            }
        }

        mmServerSocket = tmp;
    }
    public void run() {
        BluetoothSocket socket = null;

        // Listen to the server socket if we're not connected
        Log.i("BluetoothComService", String.valueOf(mState));
        while (mState != STATE_CONNECTED) {
            try {
                // This is a blocking call and will only return on a
                // successful connection or an exception
                mAdapter.cancelDiscovery();

                socket = BluetoothAdapter.getDefaultAdapter()                           
                     .getRemoteDevice(getThermMAC())
                     .createRfcommSocketToServiceRecord(UUID
                     .fromString("00001101-0000-1000-8000-00805F9B34FB"));

           //   socket = mmServerSocket.accept();  // here socket might be closed or timeout
                Log.e("BluetoothComService", "No accept Exception");
            }catch (IOException e) {
                Log.e("TAG", e.getMessage());
            }

getThermMAC() 以上是我返回绑定设备地址的方法。

错误

run(),如果我使用

socket=BluetoothAdapter.getDefaultAdapter()                           
                     .getRemoteDevice(getThermMAC())
                     .createRfcommSocketToServiceRecord(UUID
                     .fromString("00001101-0000-1000-8000-00805F9B34FB"));

然后我在Android框架中BluetoothSocket.java的第501行得到一个NPE,也就是这一行

int left = b.length;

所以看起来bnull 但它似乎不是在调试之后。 b 是一个byte[],它是从我的BluetoothService 类发送的,它从ConnectedThread 中发送一个全新的、初始化的byte[],就像sdk 示例和我在旧版本上所做的那样。

如果我将其注释掉并尝试使用

socket = mmServerSocket.accept();

过去一直在工作,然后我得到

bt socket is not in listen state

所以,我不知道如何将其置于“聆听状态”或为什么不会。有没有人经历过这个或知道如何解决它?或者如果我使用第一个 sn-p(如果那是正确的),如何避免获得 NPE

我找到了

当我收到IOException 时,我发现this post 引导我here 但这并没有让我到任何地方。

注意:赏金信息显示为 4.4.4,但在选项卡 4 上为 4.4.2

设备错误

当我第一次通过 USB 将设备连接到计算机时,我也注意到这些蓝牙错误

09-05 15:18:03.217: E/BluetoothServiceJni(15148): SOCK FLAG = 0 ***********************
09-05 15:18:13.177: E/BluetoothServiceJni(15148): SOCK FLAG = 0 ***********************
09-05 15:18:13.217: E/BluetoothServiceJni(15148): SOCK FLAG = 0 ***********************

但我无法找出那个标志的含义。

我意识到 BT 堆栈 4.x (See one of many bug reports) 中存在已知错误

minSDK 目前是 10。不过,如果我找到一个可行的解决方案,那么我可以解决这个问题。

【问题讨论】:

  • Android 在 4.3 中用 Bluedroid 替换了蓝牙堆栈,它是一个完整的掌上电脑。破坏了各种东西,包括A2DP。有没有机会用 4.2 和 4.3 平分,看看它是否在那里中断?
  • @chrylis 我很确定它适用于 4.2 不确定 4.3,atm
  • 您要连接什么类型的设备?
  • @bobbyg603 这是我们为我们制造的 SPP 设备。让我知道还有哪些其他信息可能会有所帮助。我向它发送一个命令,它会发回传感器信息。
  • 您可能应该在问题中指出您的 minSdk

标签: java android android-bluetooth


【解决方案1】:

试试 this 代码,this 在 nexus 7 上运行 android 4.4.2

private boolean refreshDeviceCache(BluetoothGatt gatt){
    try {
        BluetoothGatt localBluetoothGatt = gatt;
        Method localMethod = localBluetoothGatt.getClass().getMethod("refresh", new Class[0]);
        if (localMethod != null) {
           boolean bool = ((Boolean) localMethod.invoke(localBluetoothGatt, new Object[0])).booleanValue();
            return bool;
         }
    } 
    catch (Exception localException) {
        Log.e(TAG, "An exception occured while refreshing device");
    }
    return false;
}


    public boolean connect(final String address) {
           if (mBluetoothAdapter == null || address == null) {
            Log.w(TAG,"BluetoothAdapter not initialized or unspecified address.");
                return false;
        }
            // Previously connected device. Try to reconnect.
            if (mBluetoothGatt != null) {
                Log.d(TAG,"Trying to use an existing mBluetoothGatt for connection.");
              if (mBluetoothGatt.connect()) {
                    return true;
               } else {
                return false;
               }
        }

        final BluetoothDevice device = mBluetoothAdapter
                .getRemoteDevice(address);
        if (device == null) {
            Log.w(TAG, "Device not found.  Unable to connect.");
            return false;
        }

        // We want to directly connect to the device, so we are setting the
        // autoConnect
        // parameter to false.
        mBluetoothGatt = device.connectGatt(MyApp.getContext(), false, mGattCallback));
        refreshDeviceCache(mBluetoothGatt);
        Log.d(TAG, "Trying to create a new connection.");
        return true;

}

【讨论】:

  • 谢谢,但我不确定我是否可以使用它,因为我连接的设备不是智能设备。但是,也许如果你能解释其中的一些代码,例如它的使用位置,它会有所帮助。
  • 请注意,BluetoothGatt 仅在 API 18+ 中可用。这个答案可能应该包含这些信息。
  • @codeMagic 您可能需要考虑增加您的 minSdk。
  • @Code-Apprentice 查看我的问题的编辑。只是增加它不会做任何事情。如果有人可以告诉我 why 会使其工作以及可能如何与我当前的代码一起使用,则可以选择增加它以与 BluetoothGatt 类一起使用。据我了解,这适用于我尝试连接的设备不是低能耗设备。
  • 请解释这个答案将如何帮助 OP。
【解决方案2】:

我也使用了相同代码的连接步骤,但这仅适用于智能 Android 设备。

您可以获取要连接的特定设备的 BluetoothDevice 对象。 之后,您可以使用蓝牙耳机的连接和断开方法通过反射连接到特定的蓝牙设备。

Class<?> BTHeadset = Class.forName("android.bluetooth.BluetoothHeadset");
        Method conn = BTHeadset.getMethod("connect", new Class[] {BluetoothDevice.class});
               conn.invoke(BTHeadset, new Object[] {btDev});

您可以使用相同的代码通过反射使用断开方法断开设备。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-09
    • 2014-07-02
    • 2010-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-31
    相关资源
    最近更新 更多