其中Bluechat示例程序的一部分确实已发现的设备配对? [英] Which part of Bluechat sample program does pairing of discovered devices?

查看:228
本文介绍了其中Bluechat示例程序的一部分确实已发现的设备配对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过Android手机将命令发送到蓝牙设备(CSR8670)
我想过使用RFCOMM,我可以达到发现蓝牙装置
点 发现之后我已经用

  BluetoothDevice类设备= intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
device.createBond();
 

要配对的蓝牙芯片的Andr​​oid手机
高达配对一步似乎都工作正常,因为我可以看到芯片配对使用手机内置的蓝牙设置界面的手机,但我不知道如何着手在此之后,我似乎已经碰了壁

接下来的事情我所做的就是

  serverSocketForCSRComm = myBluetoothAdapter.listenUsingRfcommWithServiceRecord(世界,你好,MY_UUID);
 

在这里MY_UUID已被定义为

 私有静态最后UUID MY_UUID = UUID.fromString(0000FFFF-0000-1000-8000-00805F9B34FB);
 

在MainActivity类。我还要指出的是,从企业社会责任的文件不是很清楚,所以我需要通过这个工作,使用了大量的命中和审判。我曾用世界,你好和MY_UUID以上的基础上,在CSR芯片的样品code一些意见(我愿意改变这些值按所需要的参数,这个特定的UUID的选择,因为企业社会责任评价说,该设备的用途为0xFFFF的UINT16 UUID和我读到这里的,这是我应该怎么转换成16位的UUID到适当的128-bit的UUID)。此外,listenUsingRfcommWithServiceRecord()funcition似乎立即返回不管是什么,我给作为参数,所以不匹配的UUID可能不是我的问题的唯一原因。

调用上述功能之后,我称之为

  btSocketForCSRComm = serverSocketForCSRComm.accept();
 

在这个函数被调用的应用程序似乎被卡住。我相信,接受()是一个阻塞函数,并不会返回,直到到的BluetoothSocket一个建立正确的连接应该是陷入了应用程序的原因。在这一点上我有热墙等正在尝试从学习Android开发者网站上的Bluechat示例程序。

我的问题是,究竟如何配​​对发生在Bluechat程序?我可以看到发现新设备的功能调用,以及获得previously绑定设备的清单,但我不能找到一个调用createBond()函数,我不知道的code的节究竟是被用于所发现的设备的配对。它没有帮助,我没有跟我两个Android设备现在,所以我甚至不能做的Bluechat code样品运行,看它在行动。

请点我的code在不被发现的设备的实际配对,或请让我知道配对的另一种方式(通过这点我是pretty的信心Bluechat程序段它应该存在的,但我没能找到它)发现的设备除了使用createBond()函数从BluetoothDevice类类。

EDIT1(增加了澄清意见) -

 公共ConnectThread(BluetoothDevice类设备){
    Log.d(MYLOG,ConnectThread(BluetoothDevice类设备)构造函数);
    mmDevice =设备;
    的BluetoothSocket TMP = NULL;

    尝试 {
        方法方法= device.getClass()实现getMethod(createBond,(等级[])为空)。
        method.invoke(设备,(对象[])NULL);
    }赶上(例外五){
        e.printStackTrace();
    }
    做{

    }而(device.getBondState()= BluetoothDevice.BOND_BONDED!);
    Log.d(MYLOG,粘接完成);

    //获取的BluetoothSocket用于与连接
    //给BluetoothDevice类
    尝试 {
        TMP = device.createRfcommSocketToServiceRecord(MY_UUID);
    }赶上(IOException异常E){
        Log.d(MYLOG,createRfcommSocketToServiceRecord(MY_UUID)出了问题);
        Log.e(TAG,创建()失败,E);
    }
    mmSocket = TMP;
}
 

解决方案

这是不是一个答案,但若干意见。

我想对我的两个Android手机的BluetoothChat例如MOTO G(主)和三星Galaxy S III(从)。在测试之前,将高速缓存中清除,以便两个设备都不能配对。

我已经插入了一些logcat的调试信息打印的值

  

BluetoothAdapter.getDefaultAdapter()。getBondedDevices()。大小()

权利之前和下面的方法每次通话后:

  • listenUsingRfcommWithServiceRecord listenUsingInsecureRfcommWithServiceRecord
  • BluetoothServerSocket的接受()
  • createRfcommSocketToServiceRecord createInsecureRfcommSocketToServiceRecord
  • 的BluetoothSocket的连接()的getInputStream()的getOutputStream()
  • 阅读()的write()上述数据流的方法。

令人惊讶的,聊天的过程中,对 getBondedDevices报告值()。大小()总是为零。此外,Android的API指南说,

  

在建立连接与远程设备的第一次,配对请求被自动$ psented到使用者P $

     

...

     

注:如果两个设备都没有previously配对,那么Android框架会自动连接过程中表现出的配对请求通知或对话框,用户

不过,该对话框没有出现,当我第一次运行BluetoothChat应用程序。 (它没有出现在随后的运行,但这次,连接失败。)

在第一次运行之后,我退出了应用程序。每个设备留在其他的Andr​​oid的蓝牙设置可用设备列表中,但两款手机并没有配对。

我也看了看源$ C ​​$ C含在Android SDK的蓝牙文件夹中的文件。有三种方法,其名称以 createBond ,但只叫他们中的一个,并呼吁只有一次,在文件 BluetoothTestUtils.java 。当然,这并不意味着配对是永远做不完的,但显然,如果在聊天会话期间曾经发生配对,它不是通过调用执行 createBond 直接

I need to send commands through an android phone to a Bluetooth enabled device (CSR8670)
I thought about using RFCOMM and I can reach the point of discovering the bluetooth device
After discovery I have used

BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
device.createBond();  

to pair the Bluetooth chip with the android phone
Upto the pairing step seems to be working fine, as I can see that the chip is paired to the phone using the phone's builtin bluetooth settings interface, but I have no idea how to proceed after this as I seem to have hit a wall

The next thing I did was

serverSocketForCSRComm = myBluetoothAdapter.listenUsingRfcommWithServiceRecord("Hello world", MY_UUID);  

where MY_UUID has been defined as

private static final UUID MY_UUID = UUID.fromString("0000FFFF-0000-1000-8000-00805F9B34FB");  

in the MainActivity class. I should also point out that the documentation from CSR is not very clear so I need to work through this using a lot of hit and trial. I had used the arguments "Hello world" and MY_UUID above based on some comments in the CSR chip's sample code (I am open to changing these values as per needed; this specific UUID was chosen because the CSR comments say the device uses a uint16 UUID of 0xFFFF and I read here that this is how I should convert a 16-bit UUID to a proper 128-bit UUID). Also, listenUsingRfcommWithServiceRecord() funcition seems to return immediately no matter what I give as an argument, so a mismatched UUID might not be the sole cause of my problem.

After calling the above function, I call

btSocketForCSRComm = serverSocketForCSRComm.accept();  

When this function is called the app seems to get stuck. I believe that accept() is a blocking function and does not return until a proper connection to a BluetoothSocket is established which should be the reason for the app getting stuck. At this point I have hot a wall and so am trying to study the Bluechat sample program from android developer's website.

My question is, how exactly does pairing happen in the Bluechat program? I can see the function calls for discovery of new devices as well as getting a list of previously bonded devices, but I cannot find a call to createBond() function, and I am not sure which section of the code exactly is being used for pairing of the discovered devices. It does not help that I do not have two android devices with me right now, so I cannot even do a sample run of the Bluechat code to see it in action.

Please point me to the section of the code in the Bluechat program that does the actual pairing of the discovered devices, or please let me know the alternative way of pairing (by this point I am pretty confident it should exist, but am not able to find it) discovered devices besides using the createBond() function from the BluetoothDevice class.

EDIT1 (added for clarifying the comments)--

public ConnectThread(BluetoothDevice device) {
    Log.d("MYLOG", "ConnectThread(BluetoothDevice device) CONSTRUCTOR");
    mmDevice = device;
    BluetoothSocket tmp = null;

    try {
        Method method = device.getClass().getMethod("createBond", (Class[]) null);
        method.invoke(device, (Object[]) null);
    } catch (Exception e) {
        e.printStackTrace();
    }
    do{

    }while(device.getBondState()!=BluetoothDevice.BOND_BONDED);
    Log.d("MYLOG", "Bonding complete" );

    // Get a BluetoothSocket for a connection with the
    // given BluetoothDevice
    try {
        tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
    } catch (IOException e) {
        Log.d("MYLOG", "createRfcommSocketToServiceRecord(MY_UUID) GONE WRONG");
        Log.e(TAG, "create() failed", e);
    }
    mmSocket = tmp;
}

解决方案

This is not an answer, but a number of observations.

I tried the BluetoothChat example on my two Android phones, a MOTO G (master) and a Samsung Galaxy S III (slave). Before the test, the caches were cleared so that the two devices were not paired.

I have inserted some Logcat debugging messages to print the value of

BluetoothAdapter.getDefaultAdapter().getBondedDevices().size()

right before and after each call of the following methods:

  • listenUsingRfcommWithServiceRecord and listenUsingInsecureRfcommWithServiceRecord,
  • BluetoothServerSocket's accept(),
  • createRfcommSocketToServiceRecord and createInsecureRfcommSocketToServiceRecord,
  • BluetoothSocket's connect(), getInputStream() and getOutputStream(),
  • the read() and write() methods of the aforementioned data streams.

Surprisingly, during the course of chat, the reported value of getBondedDevices().size() was always zero. Furthermore, the Android API Guide says that

Once a connection is made with a remote device for the first time, a pairing request is automatically presented to the user.

...

Note: If the two devices have not been previously paired, then the Android framework will automatically show a pairing request notification or dialog to the user during the connection procedure

However, the dialog did not show up when I first run the BluetoothChat app. (It did show up on a subsequent run, but this time, connection failed.)

After the first run, I exited the app. Each device remained in the other's Android Bluetooth Settings "Available devices" list, but the two phones were not paired.

I also looked at the source code in the files containing in the Android SDK's Bluetooth folder. There are three methods whose names start with createBond, but only one of them is called, and called only once, in the file BluetoothTestUtils.java. Of course, that doesn't mean pairing is never done, but apparently, if pairing had ever occurred during the chat session, it was not performed by calling createBond directly.

这篇关于其中Bluechat示例程序的一部分确实已发现的设备配对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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