标签: android-bluetooth

基于RSSI估计信标接近度/距离 - 蓝牙LE

我有一个简单的iOS应用程序,显示它使用诸如"立即","近"等表达式检测到的蓝牙LE信标的接近度.我需要在Android上编写类似的东西.

我已经按照Android开发人员的教程进行了操作,我能够列出检测到的设备,现在想要估算距离/接近度 - 这就是它成为问题的地方.根据这个SO线程,它只是一些数学计算.但是,它们要求我提供txPower值.

根据Dave Smith的本教程(以及与此蓝牙SIG声明的交叉引用),它应该由信标设备广播为类型的"AD结构" 0x0A.所以我所做的是解析AD结构并查找与该类型匹配的有效负载.

问题:我有4个信标 - 2个estimotes和2个appflares.estimotes根本不播放txPower,appflares将它们广播为0.

这里有什么我想念的吗?iOS应用程序似乎没有任何问题处理它,但使用iOS SDK它在幕后做,所以我不知道如何产生完全相同或类似的行为.还有其他方法可以解决我的问题吗?

如果您想查看我用来解析AD结构的代码,可以从前面提到的Dave Smith的github中获取,可以在这里找到.我对该类所做的唯一更改是添加以下方法:

public byte[] getData() {

    return mData;
}
Run Code Online (Sandbox Code Playgroud)

这就是我处理扫描回调的方法:

// Prepare the callback for BLE device scan
this.leScanCallback = new BluetoothAdapter.LeScanCallback() {

    @Override
    public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {

        if (!deviceList.contains(device)) {

            MyService.this.deviceList.add(device);
            Log.e("Test", "Device: " + device.getName());

            List<AdRecord> adRecords = AdRecord.parseScanRecord(scanRecord);

            for (AdRecord adRecord : adRecords) {

                if …
Run Code Online (Sandbox Code Playgroud)

android bluetooth rssi bluetooth-lowenergy android-bluetooth

20
推荐指数
3
解决办法
7万
查看次数

如何通过蓝牙低功耗(BLE)链路发送数据?

我能够发现,连接到蓝牙.

源代码 - -

通过蓝牙连接到远程设备:

//Get the device by its serial number
 bdDevice = mBluetoothAdapter.getRemoteDevice(blackBox);

 //for ble connection
 bdDevice.connectGatt(getApplicationContext(), true, mGattCallback);
Run Code Online (Sandbox Code Playgroud)

Gatt CallBack for Status:

 private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
    //Connection established
    if (status == BluetoothGatt.GATT_SUCCESS
        && newState == BluetoothProfile.STATE_CONNECTED) {

        //Discover services
        gatt.discoverServices();

    } else if (status == BluetoothGatt.GATT_SUCCESS
        && newState == BluetoothProfile.STATE_DISCONNECTED) {

        //Handle a disconnect event

    }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) …
Run Code Online (Sandbox Code Playgroud)

android bluetooth-lowenergy android-bluetooth gatt

20
推荐指数
2
解决办法
5万
查看次数

通过蓝牙打印机Android打印Pdf文件

在我的项目中需要通过蓝牙打印机打印Pdf文件.我写了一个代码,通过pdf打印它适用于文本,

但我想在蓝牙打印机上打印PDF文件.

我的java代码打印文本

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv= (ListView) findViewById(R.id.listView1);
     //   listdata(lv);
        try {

        // we are goin to have three buttons for specific functions
        Button openButton = (Button) findViewById(R.id.open);
        Button sendButton = (Button) findViewById(R.id.send);
        Button closeButton = (Button) findViewById(R.id.close);
        Button btnco= (Button) findViewById(R.id.btnconnect);

        btnco.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try {
                    findBT();
                    openBT();
                } catch (Exception ex) {
                }
            }
        });
        myLabel = (TextView) findViewById(R.id.label);
        myTextbox = …
Run Code Online (Sandbox Code Playgroud)

android bluetooth android-bluetooth android-print-framework

20
推荐指数
1
解决办法
8149
查看次数

在Android上连接Xamarin的配对蓝牙设备

当应用程序通过A2DP或Hands Free Profile启动时,我们需要我们的应用程序能够自动连接到配对的蓝牙设备.

我们正在使用Xamarin(monodroid),用于Android平台.

我发现了这个stackoverflow问题:以编程方式连接到配对的蓝牙设备

但它与本土方法有关(见kcoppock的回答).我想知道是否有办法通过Xamarin实现这一目标.我们可以连接到SPP端点,因为它是基于RFCOMM的连接,但我们需要它和音频连接,所以我们想要一种连接到A2DP的方法.

更新1:

我们尝试使用如下CreateInsecureRfcommSocketToServiceRecord方法连接:

mmSocket = device.CreateInsecureRfcommSocketToServiceRecord(0000110A-0000-1000-8000-00805F9B34FB); mmSocket.Connect();

在致电Connect时,我们收到错误消息:

read failed, socket might closed or timeout, read ret: -1

堆栈跟踪始于:

Java.IO.IOException at Android.Runtime.JNIEnv.CallVoidMethod (IntPtr jobject, IntPtr jmethod) [0x00062] in /Users/buil…

更新2:

顺便说一句,当我们尝试使用kcoppock的方法通过本机java测试应用程序连接时,连接代码似乎没有错误,但设备没有作为A2DP耳机连接.

我们看到能够做到的唯一编程方式Google Play应用,它证明了这是可能的.

android bluetooth xamarin.android xamarin android-bluetooth

19
推荐指数
1
解决办法
1万
查看次数

Android广播接收器蓝牙事件捕捉

我正试图用广播接收器捕捉蓝牙状态变化.

我的清单:

<uses-permission android:name="android.permission.BLUETOOTH" />
<application>
     <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".BluetoothBroadcastReceiver"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
            <action android:name="android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED" />
            <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
            <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
        </intent-filter>
    </receiver>
</application>
Run Code Online (Sandbox Code Playgroud)

接收onReceive方法:

public void onReceive(Context context, Intent intent) {

    String action = intent.getAction();
    Log.d("BroadcastActions", "Action "+action+"received");
    int state;
    BluetoothDevice bluetoothDevice;

    switch(action)
    {
        case BluetoothAdapter.ACTION_STATE_CHANGED:
            state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
            if (state == BluetoothAdapter.STATE_OFF)
            {
                Toast.makeText(context, "Bluetooth is off", Toast.LENGTH_SHORT).show();
                Log.d("BroadcastActions", "Bluetooth is …
Run Code Online (Sandbox Code Playgroud)

android bluetooth android-broadcast android-bluetooth

19
推荐指数
1
解决办法
5万
查看次数

BLE Android - 未调用onConnectionStateChange

我在尝试连接外围设备时遇到问题.有时回调onConnectionStateChange(...)不会被调用BluetoothDevice#connectGatt(...).我想要实现的是用户操作触发的快速和短连接.

这种情况大约发生在10次,没有特定的事先行动.它持续约20至30秒,或直到应用程序被杀死并重新打开.我遵循的正常步骤顺序是:

  1. 扫描设备以查找外围设备.
  2. 打电话BluetoothDevice#connectGatt(...).如果连接时间超过1秒,则表示连接"卡住",因此无法连接,因此BluetoothDevice#connectGatt(...)再次调用.这是在5次尝试的限制下完成的.
  3. onConnectionStateChange(...)使用newStateCONNECTED 调用并开始服务发现.
  4. 其余的操作没有问题.
  5. 断开连接后BluetoothGatt#close()调用.

发生的问题是在第3点.有时onConnectionStateChange(...)不会被调用.我注意到大多数时候问题都是从特定的行为开始的.之后BluetoothDevice#connectGatt(...),onConnectionStateChange(...)使用newStateCONNECTED 调用,但几乎立即(~40毫秒)再次使用newStatusDISCONNECTED 调用.由于状态改变的时间很短,我可以推断设备甚至没有尝试建立连接并将状态更改为DISCONNECTED.问题在以下时间结束:

  1. 20-30秒过去了.在此期间onConnectionStateChange(...)永远不会被召唤.问题结束时,onConnectionStateChange(...)称为应用尝试连接的次数.例如,如果BluetoothDevice#connectGatt(...)被调用15次,onConnectionStateChange(...)则被调用15次,newState等于DISCONNECTED.这很奇怪,因为在任何连接尝试中都没有将状态更改为CONNECTED.
  2. 该应用程序被杀死并重新开始.

SDK18和SDK 21中发生此错误.

@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
    String deviceName = device.getName();
    if (deviceName == null) return;
    Log.d("BLUETOOTH CONNECTION", "Device found: " + device.getName());
    if (mMode == SCAN_MODE) { …
Run Code Online (Sandbox Code Playgroud)

java android bluetooth-lowenergy android-bluetooth

18
推荐指数
1
解决办法
1万
查看次数

如何使用android从蓝牙耳机捕获关键事件

我的应用程序可以通过普通耳机控制.它只是覆盖"onKeyDown".但是没有捕获蓝牙耳机的关键事件 - 为什么?或者如何捕获蓝牙键事件?

如果我按下耳机上的按钮,"log cat"会显示以下内容:

Bluetooth AT recv(3043): AT+VGS=15
AudioPolicyManagerBase(13654): FM radio recording off
AudioService(2261): sendVolumeUpdate, isKeyguardLocked...Not to update Volume Panel.
VolumePanel(2261): change volume by MSG_VOLUME_CHANGED
VolumePanel(2261): onVolumeChanged(streamType: 6, flags: 0)
VolumePanel(2261): Call setChangeSeekbarColor(false)
Run Code Online (Sandbox Code Playgroud)

我也尝试处理媒体按钮操作,但这不起作用.我的想法是一个免费的可配置键映射:用户选择"设置键"我的应用程序听到所有键(硬件,媒体按钮,蓝牙耳机),然后用户按下一个键,事件/密钥代码存储在配置中.

总结不工作的答案: 音量按钮必须由"VOLUME_CHANGED_ACTION"捕获.问题是这个意图被广播到其他应用程序并且abortBroadcast()不起作用(它仅适用于"有序"广播).另一个问题是有线耳机和手机上的按键触发onReceive()两次(为什么?)蓝牙耳机会触发一次.下一个问题是蓝牙耳机上的第3个键.它触发语音命令(s-s在s3上启动),我试图捕获许多不同的意图,但我不能"接收"这个按钮按下而不知道为什么.最后,我希望捕获各种按钮,并且不希望它们由其他应用程序处理(例如使用onKeyDown并返回true).

android bluetooth android-bluetooth

17
推荐指数
2
解决办法
3万
查看次数

BlueZ vs Bluedroid蓝牙堆栈

BlueZ有很多文档和所有文档.同样,我了解BlueZ支持A2DP接收器支持.同时,通过修改同一个内部的audio.conf文件,可以使android设备充当A2DP接收器.

我也知道从android 4.2开始,BlueZ堆栈被Broadcom的Bluedroid堆栈取代.我搜索了很多关于Bluedroid堆栈的信息.但我无法找到相同的细节.

希望有人可以帮我解决以下疑问.

1).Bluedroid是否支持A2DP接收器?

2).audio.conf文件是否可用于具有bluedroid堆栈的设备?

3).我将通过直接调用BlueZ堆栈的API来为Android设备编写A2DP接收器支持.但我最后的问题是Bluedroid堆栈是否也可以使用相同的?希望Bluedroid只是BlueZ的扩展.

请帮忙.

android bluetooth a2dp bluez android-bluetooth

17
推荐指数
1
解决办法
3万
查看次数

BLE蓝牙低功耗设备中的身份验证问题

我们正在制造一个带有BLE接口的物联网设备,该接口使用托管芯片CC2541 的HM-11(http://www.seeedstudio.com/wiki/Bluetooth_V4.0_HM-11_BLE_Module)分线板(http://www.ti. com/product/CC2541).

身份验证方法设置为 2:Auth with PIN

显示可用身份验证模式的数据表中的剪辑如下:

63. Query/Set Module Bond Mode Send Receive Parameter AT+TYPE? OK+Get:[para1] None AT+TYPE[para1] OK+Set:[para1] Para1: 0~2 0:Not need PIN Code 1:Auth not need PIN 2:Auth with PIN 3:Auth and bond Default: 0

对于低于Android 5.0版本的设备,它可以正常工作.

然而

  1. 对于Android版本5.0的设备,配对对话框显示没有diaplyed-pin或pin-enter-field,当pair点击按钮时,它无法配对 - 抱怨

    由于PIN或密钥不正确,无法与MyApp配对.

  2. 对于使用Android版本5.1的设备,它甚至不会显示配对对话框,也无法配对.

注意:尝试重启设备,忘记设备,清除设备的绑定信息.

寻找指导,建议,帮助,评论,代码.

android bluetooth-lowenergy android-bluetooth

17
推荐指数
1
解决办法
1810
查看次数

BLE扫描的解决方案是SCAN_FAILED_APPLICATION_REGISTRATION_FAILED?

我的Android应用程序扫描BLE设备,从某一点开始,它开始失败,错误代码为2(ScanCallback.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED).我正在使用Nexus 9,5.0.1 Lollipop.

即使在我重新启动应用程序后,这个问题仍然存在,当我从"设置"重新启动蓝牙服务时,我终于可以摆脱这个问题了.但是这个问题反复发生,我认为我编码错误; BLE相关的API是新的,信息很少.

有没有人知道这个错误的一般解决方案,最好不要求重启蓝牙服务?即使Android API参考中记录了此错误代码,我也不知道如何正确处理它.

bluetooth-lowenergy android-bluetooth android-5.0-lollipop

16
推荐指数
2
解决办法
4556
查看次数