android 蓝牙4.0重连,与嵌入式设备的Android 4.0蓝牙连接错误:“...

我有以下设置:

Android设备使用“客户端”套接字连接到远程嵌入式设备,Android应用程序使用以下代码段连接到嵌入式设备.

在嵌入式设备上使用MindTree BT堆栈,其中根据设备中的某些属性准备了服务器串行套接字,而这些属性是Android应用程序所熟悉的,因此嵌入式设备上定义的连接不受保护!

两种应用程序的组合适用于:

> 2种不同型号的LG手机(版本代码< 10使用“常规方法”)

> 2种HTC的不同型号(版本代码< 10使用“替代方法”)

> Pantech平板电脑(版本代码< 13使用“解决方法”)

今天,我已经在三星S3,摩托罗拉MB886和Nexus 7上试用了该应用程序…

调用socket.connect()时,所有操作均导致“权限被拒绝” …(清单中我具有适当的权限,否则在其他设备上将无法使用.)

我测试过的所有新设备都是版本代码> 4.0,所以我想知道:

有人知道API的任何更改吗?

也许Android 4.0强制了安全性?

似乎错误发生在Bonding状态,因为我可以在嵌入式程序日志中看到…

有什么见解吗?

编码:

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

+ "

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值