Android 蓝牙 connect() 抛出错误

2024-03-02

我有一个项目要求,其中我们有一个包含两个组件的产品 - 一个 Android 平板电脑和一个 PCB(包含 RN42 蓝牙芯片)。这两个组件位于塑料外壳内,因此总是彼此靠近。这两个组件设备最初会配对一次,因此配对信息将永远存在于平板电脑中。

Problem: 在我的代码中,我使用一个函数来初始化蓝牙套接字并访问 DataOut 和 DataIn 端口,使用这些端口可以在平板电脑和 PCB 之间交换数据。

案例1:当我使用三星S2(4.1.2)和PCB时,这段代码100%有效,

Case2: BUT当我将平板电脑(iball 3G 7271(4.1.2))与印刷电路板一起使用时,大多数时候我在连接到插座时遇到问题。我收到错误说明-

 -Service discovery failed,
 -Connection is not created (failed or aborted).
 -Resource is busy

在大多数情况下。

我尝试过的:

  • 尝试使用反射,但没有成功。
  • 已使用通用UUID - 00001101-0000-1000-8000-00805F9B34FB,但没有用。

我不明白为什么当我使用带有 RN42 芯片的三星 S2 时代码工作正常,但当我使用带有 RN42 芯片的 iball 平板电脑时代码会抛出错误。在进一步挖掘时,我发现了这篇文章http://redacacia.me/2012/07/17/overcoming-android-bluetooth-blues-with-reflection-method/#comment-1414 http://redacacia.me/2012/07/17/overcoming-android-bluetooth-blues-with-reflection-method/#comment-1414据报道,中国设备(iball 就是其中之一)使用 MTK 处理器,并且存在蓝牙连接问题。

下面是我在设备和 RN42 芯片之间进行蓝牙连接的代码


public class AppFunctions {

    public static BluetoothAdapter mBluetoothAdapter;
    public static BluetoothAdapter iballAdapter = null;
    public static BluetoothSocket Socket = null;
    public static BluetoothDevice RN42_Device;
    private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

    private static String address = "00:06:66:49:57:60";    // MAC of RN42 bluetooth chip on PCB
    public static OutputStream DataOut = null;
    public static InputStream DataIn = null;

    //Function to turn on BT socket and to initialize it
    public static void setBluetooth(Context context) {

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        mBluetoothAdapter.disable(); //Disable bluetooth on device
        mBluetoothAdapter.enable();  //Enable bluetooth on device

        Timer mTimer = new Timer();
        mTimer.schedule(new TimerTask()
        {
            @Override
            public void run()
            {

                iballAdapter = BluetoothAdapter.getDefaultAdapter();
                Log.i("AppFuntions", "iballAdapter - " + iballAdapter);
                RN42_Device = iballAdapter.getRemoteDevice(address);
                Log.i("AppFuntions", "RN42_Device - " + RN42_Device);

                try {
                    Socket = RN42_Device.createRfcommSocketToServiceRecord(MY_UUID);

    /*              tried reflection,but no use
                    Method m = RN42_Device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
                    Socket = (BluetoothSocket) m.invoke(RN42_Device, 5);*/

                    resetConnection();
                    connect();

                    Log.i("AppFunctions", "DataOut -" + DataOut);
                    Log.i("AppFunctions", "DataIn -" + DataIn);
                } catch (Exception e) {e.printStackTrace();}

                }
        }, 10000);
    }

    private static void resetConnection() {
        if (DataIn != null) {
            try {DataIn.close();} catch (Exception e) {}
            DataIn = null;
        }

        if (DataOut != null) {
            try {DataOut.close();} catch (Exception e) {}
            DataOut = null;
        }

        if (Socket != null) {
            try {Socket.close();} catch (Exception e) {}
            Socket = null;
        }
    }

    private static boolean connect() {

        // Reset all streams and socket.
        resetConnection();

        if (RN42_Device == null)
            RN42_Device = iballAdapter.getRemoteDevice(address);

        // Make an RFCOMM binding.
        try {
            Socket = RN42_Device.createRfcommSocketToServiceRecord(MY_UUID);

/*          tried reflection,but no use
            Method m = RN42_Device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
            Socket = (BluetoothSocket) m.invoke(RN42_Device, 5);*/

        } catch (Exception e1) {e1.printStackTrace();return false;}

        try {

            Socket.connect(); // Fails here most of the time for iball tablet and RN42,though works always for samsung S2 and Rn42
        } catch (Exception e) {e.printStackTrace();return false;}

        try {
            DataOut = Socket.getOutputStream();
            DataIn = Socket.getInputStream();
        } catch (Exception e) {e.printStackTrace();return false;}

        return true;
    }
}

三星 s2 的日志(一直工作正常):

 I/AppFuntions(21103): iballAdapter - android.bluetooth.BluetoothAdapter@41fe9250
 I/AppFuntions(21103): RN42_Device - 00:06:66:49:57:60
 V/BluetoothSocket.cpp(21103): initSocketNative
 V/BluetoothSocket.cpp(21103): ...fd 66 created (RFCOMM, lm = 26)
 V/BluetoothSocket.cpp(21103): initSocketFromFdNative
 I/BluetoothConn(21103): connecting to Socket
 V/BluetoothSocket.cpp(21103): abortNative
 V/BluetoothSocket.cpp(21103): ...asocket_abort(53) complete
 V/BluetoothSocket.cpp(21103): destroyNative
 V/BluetoothSocket.cpp(21103): ...asocket_destroy(53) complete
 V/BluetoothSocket.cpp(21103): abortNative
 V/BluetoothSocket.cpp(21103): ...asocket_abort(66) complete
 V/BluetoothSocket.cpp(21103): destroyNative
 V/BluetoothSocket.cpp(21103): ...asocket_destroy(66) complete
 V/BluetoothSocket.cpp(21103): initSocketNative
 V/BluetoothSocket.cpp(21103): ...fd 53 created (RFCOMM, lm = 26)
 V/BluetoothSocket.cpp(21103): initSocketFromFdNative
 D/BluetoothUtils(21103): isSocketAllowedBySecurityPolicy start : device null
 V/BluetoothSocket.cpp(21103): connectNative
 V/BluetoothSocket.cpp(21103): ...connect(53, RFCOMM) = 0 (errno 115)
 I/AppFunctions(21103): DataOut -android.bluetooth.BluetoothOutputStream@41e8b688
 I/AppFunctions(21103): DataIn -android.bluetooth.BluetoothInputStream@41e95790

使用 MKT 系列处理器记录 iball 平板电脑:(logcat 错误之一):

 I/AppFuntions(10598): iballAdapter - android.bluetooth.BluetoothAdapter@426ebb88
 I/AppFuntions(10598): RN42_Device - 00:06:66:49:57:60
 I/BluetoothSocket_MTK(10598): [JSR82] Bluetooth Socket Constructor
 I/BluetoothSocket_MTK(10598): [JSR82] type=1 fd=-1 auth=true encrypt=true port=-1
 I/BluetoothConn(10598): connecting to Socket
 I/BluetoothSocket_MTK(10598): [JSR82] close
 I/BluetoothSocket_MTK(10598): [JSR82] readLock got.
 I/BluetoothSocket_MTK(10598): [JSR82] Bluetooth Socket Constructor
 I/BluetoothSocket_MTK(10598): [JSR82] type=1 fd=-1 auth=true encrypt=true port=-1
 I/BluetoothSocket_MTK(10598): [JSR82] connect: do SDP
 I/BluetoothSocket_MTK(10598): [JSR82] SdpHelper::onRfcommChannelFound: channel=1
 I/BluetoothSocket_MTK(10598): [JSR82] connect: do SDP done; mPort=1
 W/System.err(10598): java.io.IOException: [JSR82] connect: Connection is not created (failed or aborted).
 W/System.err(10598):   at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:395)
 W/System.err(10598):   at com.example.guardmonitor.commons.AppFunctions.connect(AppFunctions.java:193)
 W/System.err(10598):   at com.example.guardmonitor.commons.AppFunctions.access$3(AppFunctions.java:176)
 W/System.err(10598):   at com.example.guardmonitor.commons.AppFunctions$1.run(AppFunctions.java:109)
 W/System.err(10598):   at java.util.Timer$TimerImpl.run(Timer.java:284)
 I/AppFunctions(10598): DataOut -null
 I/AppFunctions(10598): DataIn -null

请告诉我 :

  • 我做错了什么/我应该做什么来解决我的 iball 平板电脑与 Rn42 芯片的连接问题。
  • 任何有用的资源都可以分享。

提前致谢!


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 蓝牙 connect() 抛出错误 的相关文章

随机推荐

  • 标题中单个单词的颜色与组的颜色相匹配

    我最近在 经济学人 上看到了一张折线图 其中标题包含彩色单词以匹配折线图中使用的组的颜色 https www economist com blogs graphicdetail 2018 04 daily chart 1 我想知道如何使用
  • Golang SQL 查询变量替换

    我有 sql 查询需要变量替换才能更好地消耗我的go kit https github com go kit kit服务 I have dep org作为我的休息服务一部分的用户输入 例如 dep abc and org def 我尝试过一
  • “未捕获的引用错误:JQueryValidatorUI 未定义”?

    使用 jquery validation ui 插件时 未捕获的 ReferenceError JQueryValidatorUI 未定义 也未捕获类型错误 对象 对象对象 没有方法 验证 这是我的脚本顺序
  • 如何在JUNG中添加具有相同标签(但端点不同)的两条边?

    如何添加具有相同标签但端点不同的两条边 例如 我想添加两条具有相同标签 label1 的边 一条从顶点 v 1 到顶点 v 2 另一条从顶点 v 2 到 v 3 部分代码是 g addEdge label1 v 1 v 2 g addEdg
  • 如何将 javascript 对象发送到远程 CFC 组件

    我创建了一个 javascript 对象 var spanglist one q1 two q2 three q3 four q4 我创建 ajax jquery 对象以将数据发送到 CFC ajax url gridly componen
  • Angularjs:ReferenceError:范围未定义

    我是 Angularjs 的初学者 在理解模块和范围方面有一些困难 我不断收到范围未定义的错误 但我不明白为什么 首先 我将控制器链接到设置路线的位置 但由于控制器内的函数是在提交按钮上调用的 因此单击我将其拿走 我试过把它放回去 但这没有
  • pytest从不同的测试文件独立导入相同的模块

    以下主题模块包含两个函数 其中之一操作全局变量 mod py def global setter global x x 123 print setter x x def global getter print getter x x 每个功能
  • 如何在magento的成功页面中动态集成JS代码

    我知道 success phtml 是我应该放置我想要执行的代码的文件 但是我从 CJ 收到这个文件 它不是 html 而是一个 php 类 问题很简单 我想知道如何在收到订单后将此文件集成到 success phtml 中 谢谢 clas
  • np.ndarray`“is”中的奇怪行为

    is 内置运算符显示元素的奇怪行为np ndarray 尽管右侧和左侧的 id 相同 但 is 运算符返回 False 此行为特定于np ndarray a np array 1 b a view print id a 0 id b 0 T
  • postgres 使用 join 更新

    我正在尝试使用 ht 中的数据更新表 tr 两者都有几乎相同的列 所以为了测试我运行了这个查询 SELECT FROM tr a RIGHT OUTER JOIN ht b USING date name ft WHERE ft IS NO
  • 判断设备是否有触摸屏

    我的应用程序可以在标准手机上运行 但它也可以在 Android 播放器上运行 我通过 HDMI 将其连接到电视并使用鼠标进行导航 有没有办法以编程方式确定设备是否支持触摸屏 以便我可以区分两种导航方式 I tried this http d
  • 从项目 azure devops REST API 获取所有工作项

    我正在使用 Azure Devops API 通过 AWS Lambda node js 创建通知机器人 此时 我需要检查每个任务工作项是否附加到父用户故事 第一步是获取 给定 项目上的所有任务工作项 对于这一步 我正在阅读 azure d
  • 最坏情况时间复杂度分析伪代码

    有人可以帮我分析这个伪代码的时间复杂度吗 我正在寻找最坏情况的复杂度 但我无法弄清楚它是 O n 4 O n 5 还是完全其他的东西 如果您能详细说明您是如何解决这个问题的 我们将不胜感激 sum 0 for i 1 to n do for
  • 在构造函数中创建一个指针,指向 C++ 中类的实例

    我对这些 C 指针有点困惑 我想知道是否可以在构造函数内创建一个指向类实例的指针 以下行中的内容 class Room public Room Room private Room ptrToSelf Room Room ptrToSelf
  • zeppelin hive 解释器抛出 ClassNotFoundException

    我已经部署了zeppelin 0 6并在Jdbc解释器下配置了hive 尝试执行 hive show databases Throws org apache hive jdbc HiveDriver 类 java lang ClassNot
  • 如何访问封装在类中的未命名“枚举类”?

    class A public enum class HELLO WORLD 众所周知 在一个class 声明一个简单的enum 而不是enum class 是一个更好的主意 因为它已经用class鉴别 但上面的说法仍然是有效的C 0x签名
  • 将字符串写入文本文件

    我正在将日志保存到 SD 卡上的 txt 文件中 但是一旦保存了两行 它就会覆盖它并重新开始 这是我的代码 public static String getTimestamp try SimpleDateFormat dateFormat
  • 从Python中的一行中提取特定的子字符串

    我有一个包含多行格式的文件 格式如下 DIV ID 0X78800009 EXT LOS ANGELES TY STANDARD OWN 0X74400002 ABBR LA 我需要提取 EXT 值 但只提取引号中的部分 我目前正在使用这个
  • 在 Spark 上执行多个 SQL 查询

    我在文件 test sql 中有一个 Spark SQL 查询 CREATE GLOBAL TEMPORARY VIEW VIEW 1 AS select a b from abc CREATE GLOBAL TEMPORARY VIEW
  • Android 蓝牙 connect() 抛出错误

    我有一个项目要求 其中我们有一个包含两个组件的产品 一个 Android 平板电脑和一个 PCB 包含 RN42 蓝牙芯片 这两个组件位于塑料外壳内 因此总是彼此靠近 这两个组件设备最初会配对一次 因此配对信息将永远存在于平板电脑中 Pro