安卓4.0+蓝牙连接错误到嵌入式设备:"被拒绝的权限和QUOT; [英] Android 4.0+ Bluetooth connection error to an embedded device: "Permission Denied"

查看:234
本文介绍了安卓4.0+蓝牙连接错误到嵌入式设备:"被拒绝的权限和QUOT;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下设置:

这是Android设备采用的是'客户'插槽可以连接到远程的嵌入式设备,而Android应用程序使用以下code段连接到嵌入式设备。

在嵌入式设备使用MindTree BT栈,其中服务器串行插座是根据在装置中,该机器人的应用是熟悉,嵌入式设备上定义的连接,可以不固定某些属性ppared $ P $ !!

这两个应用程序的组合,适用于:

  • 2 LG手机不同型号(版本$ C $℃下10 使用了正常方式
  • 2宏达电不同型号(版本$ C $℃下10 使用了变通办法
  • 泛泰平板电脑(版本$ C $℃下13 使用了变通办法

今天,我已经试过三星S3,摩托罗拉MB886的应用程序,一台Nexus 7 ... 所有导致权限被拒绝打电话来的时候 Socket.connect()的 ...(我在清单中的适当的权限,否则就不能在其他设备使用。)

所有我已经测试了新的设备版本code> 4.0,所以我想知道:

有谁知道在API中的任何变化? 也许安卓4.0+部队的安全?

这似乎是错误发生在键合状态,因为我可以在嵌入式程序日志,看看...

任何见解?

在code:

 公众最终同步INT connectToDevice(INT connectingMethod)
        抛出BluetoohConnectionException {
    如果(插座!= NULL)
        抛出新BadImplementationException(错误插槽不为空!);
    连接=真正的;
    LOGINFO(+  -  +连接设备...);

    尝试 {
        lastException = NULL;
        lastPacket = NULL;
        如果(connectingMethod == BluetoothModule.BT_StandardConnection
                || connectingMethod == BluetoothModule.BT_ConnectionTBD)
            尝试 {

                插座= fetchBT_Socket_Normal();
                connectToSocket(插座);
                listenForIncomingSPP_Packets();
                onConnetionEstablished();
                返回BluetoothModule.BT_StandardConnection;
            }赶上(BluetoohConnectionException E){
                插座= NULL;
                如果(connectingMethod == BluetoothModule.BT_StandardConnection){
                    扔ê;
                }
                logWarning(错误创建插座!,E);
            }
        如果(connectingMethod == BluetoothModule.BT_ReflectiveConnection
                || connectingMethod == BluetoothModule.BT_ConnectionTBD)
            尝试 {
                插座= fetchBT_Socket_Reflection(1);
                connectToSocket(插座);
                listenForIncomingSPP_Packets();
                onConnetionEstablished();
                返回BluetoothModule.BT_ReflectiveConnection;
            }赶上(BluetoohConnectionException E){
                插座= NULL;
                如果(connectingMethod == BluetoothModule.BT_ReflectiveConnection){
                    扔ê;
                }
                logWarning(错误创建插座!,E);
            }
        抛出新BluetoohConnectionException(错误创建RFCOMM插座BT设备:+本
                +\ñBAD connectingMethod ==+ connectingMethod);
    } 最后 {
        连接= FALSE;
    }
}

保护无效onConnetionEstablished(){
    LOGINFO(+  -  +建立连接);
}

私人同步无效listenForIncomingSPP_Packets(){
    如果(socketListeningThread!= NULL)
        抛出新BadImplementationException(已lisening Socket的英国电信设备+本);
    LOGINFO(+  -  +监听传入数据包);
    socketListeningThread =新主题(socketListener,数据包监听器 - + bluetoothDevice.getName());
    socketListeningThread.start();
}

私有的BluetoothSocket fetchBT_Socket_Normal()
        抛出BluetoohConnectionException {
    尝试 {
        LOGINFO(+  -  +撷取BT RFCOMM插座标准UUID:+ UUID +...);
        返回bluetoothDevice.createRfcommSocketToServiceRecord(UUID.fromString(UUID));
    }赶上(例外五){
        抛出新BluetoohConnectionException(错误获取BT RFCOMM插座!,E);
    }
}

私有的BluetoothSocket fetchBT_Socket_Reflection(INT connectionIndex)
        抛出BluetoohConnectionException {
    法米;
    尝试 {
        LOGINFO(+  -  +撷取BT RFCOMM插座解决办法索引+ connectionIndex +...);
        M = bluetoothDevice.getClass()实现getMethod(createRfcommSocket,新的等级[] {} int.class)。
        返程(的BluetoothSocket)m.invoke(BluetoothDevice类,connectionIndex);
    }赶上(例外五){
        抛出新BluetoohConnectionException(错误获取BT RFCOMM插座!,E);
    }
}

私人无效connectToSocket(的BluetoothSocket插座)
        抛出BluetoohConnectionException {
    尝试 {
        LOGINFO(+  -  +连接到插座......);
        Socket.connect()的;
        LOGINFO(+  -  +连接到插座);
    }赶上(IOException异常E){
        尝试 {
            socket.close();
        }赶上(IOException异常E1){
            LOGERROR(错误而关闭套接字,E1);
        } 最后 {
            插座= NULL;
        }
        抛出新BluetoohConnectionException(错误连接到插座设备+这一点,E);
    }
}
 

解决方案

在调查此事的很长很长一段时间,我发现了一个错误的原因......一些汽车蓝牙对等不启用Android设备/不允许的。

因此​​,显然除了两个连接方法,也有两个蓝牙适配器实现的方法,一将抛出意图要求系统打开适配器,另一个是调用到BluetoothAdapter.enable( )法,使蓝牙默默地

第一种方法,会弹出一个确认对话框,并要求用户交互,而另一个没有,虽然没有显示蓝牙启用确认对话框,也对等确认没有显示出来,从而导致连接错误。

使用的第一个适配器实现的方法解决这个问题的大部分设备,如的Nexus 7,三星S3,和其他几个人,但在某些设备上仍然有一个问题,我真的不知道为什么,但是这是好多了,因为很多设备目前正与新的实现。

I have the following setup:

An Android device uses a 'Client' socket to connect to a remote embedded device, The Android application uses the following code snippet to connect to the embedded device.

On the embedded device uses MindTree BT stack, where server serial socket is prepared according to some properties in the device, which the Android application is familiar with, the connection defined on the embedded device, is not secured!!

The combination of both applications works on:

  • 2 LG phones different models (version code < 10 uses the "Normal method")
  • 2 HTC's different models (version code < 10 uses the "Workaround method")
  • Pantech Tablet (version code < 13 uses the "Workaround method")

Today, I've tried the application on Samsung S3, Motorola MB886, and a Nexus 7... All resulted in a "Permission Denied" when calling to socket.connect()... (I have the proper permissions in the manifest, otherwise it would not work on the other devices.)

All the new devices I've tested on are version code > 4.0, so I'm wondering:

Does anyone know about any changes in the API? Perhaps Android 4.0+ forces security?

It seem that the error occur in the Bonding state, since I can see on the embedded program logs...

Any insights?

The code:

public final synchronized int connectToDevice(int connectingMethod)
        throws BluetoohConnectionException {
    if (socket != null)
        throw new BadImplementationException("Error socket is not null!!");
    connecting = true;
    logInfo("+---+ Connecting to device...");

    try {
        lastException = null;
        lastPacket = null;
        if (connectingMethod == BluetoothModule.BT_StandardConnection
                || connectingMethod == BluetoothModule.BT_ConnectionTBD)
            try {

                socket = fetchBT_Socket_Normal();
                connectToSocket(socket);
                listenForIncomingSPP_Packets();
                onConnetionEstablished();
                return BluetoothModule.BT_StandardConnection;
            } catch (BluetoohConnectionException e) {
                socket = null;
                if (connectingMethod == BluetoothModule.BT_StandardConnection) {
                    throw e;
                }
                logWarning("Error creating socket!", e);
            }
        if (connectingMethod == BluetoothModule.BT_ReflectiveConnection
                || connectingMethod == BluetoothModule.BT_ConnectionTBD)
            try {
                socket = fetchBT_Socket_Reflection(1);
                connectToSocket(socket);
                listenForIncomingSPP_Packets();
                onConnetionEstablished();
                return BluetoothModule.BT_ReflectiveConnection;
            } catch (BluetoohConnectionException e) {
                socket = null;
                if (connectingMethod == BluetoothModule.BT_ReflectiveConnection) {
                    throw e;
                }
                logWarning("Error creating socket!", e);
            }
        throw new BluetoohConnectionException("Error creating RFcomm socket for BT Device:" + this
                + "\n BAD connectingMethod==" + connectingMethod);
    } finally {
        connecting = false;
    }
}

protected void onConnetionEstablished() {
    logInfo("+---+ Connection established");
}

private synchronized void listenForIncomingSPP_Packets() {
    if (socketListeningThread != null)
        throw new BadImplementationException("Already lisening on Socket for BT Device" + this);
    logInfo("+---+ Listening for incoming packets");
    socketListeningThread = new Thread(socketListener, "Packet Listener - " + bluetoothDevice.getName());
    socketListeningThread.start();
}

private BluetoothSocket fetchBT_Socket_Normal()
        throws BluetoohConnectionException {
    try {
        logInfo("+---+ Fetching BT RFcomm Socket standard for UUID: " + uuid + "...");
        return bluetoothDevice.createRfcommSocketToServiceRecord(UUID.fromString(uuid));
    } catch (Exception e) {
        throw new BluetoohConnectionException("Error Fetching BT RFcomm Socket!", e);
    }
}

private BluetoothSocket fetchBT_Socket_Reflection(int connectionIndex)
        throws BluetoohConnectionException {
    Method m;
    try {
        logInfo("+---+ Fetching BT RFcomm Socket workaround index " + connectionIndex + "...");
        m = bluetoothDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
        return (BluetoothSocket) m.invoke(bluetoothDevice, connectionIndex);
    } catch (Exception e) {
        throw new BluetoohConnectionException("Error Fetching BT RFcomm Socket!", e);
    }
}

private void connectToSocket(BluetoothSocket socket)
        throws BluetoohConnectionException {
    try {
        logInfo("+---+ Connecting to socket...");
        socket.connect();
        logInfo("+---+ Connected to socket");
    } catch (IOException e) {
        try {
            socket.close();
        } catch (IOException e1) {
            logError("Error while closing socket", e1);
        } finally {
            socket = null;
        }
        throw new BluetoohConnectionException("Error connecting to socket with Device" + this, e);
    }
}

解决方案

After very long long time of investigating the matter I've found one reason for the error... on some Android devices the auto Bluetooth peering is not enabled/allowed.

So, apparently except for two connection method, there are also two Bluetooth adapter enabling method, one would be to throw an intent to ask the system to turn the adapter on, and the other is to call onto the BluetoothAdapter.enable() method, which enables the Bluetooth silently.

The first method, pops a confirmation dialog, and require user interaction while the other does not, and while not showing the Bluetooth enabling confirmation dialog, also the peering confirmation is not shown, which causes the connection error.

Using the first adapter enabling method solves the problem on most of the devices, like the Nexus 7, Samsung S3, and a few others, but on some devices there is still an issue, and I'm not really sure why, but this is much better since many devices are now working with the new implementation.

这篇关于安卓4.0+蓝牙连接错误到嵌入式设备:&QUOT;被拒绝的权限和QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆