标签: android-bluetooth

如何解决在Android中发生的BluetoothGatt:android.os.DeadObjectException错误?

我下面的页面蓝牙低耗能为发展中国家的Android 4.3蓝牙低能量.

我已经可以打开蓝牙,扫描设备并连接到BLE设备.

但它显示了BluetoothGatt : android.os.DeadObjectException我尝试连接到设备之后(device.connectGatt),然后发现 (mBluetoothGatt.discoverServices) BLE设备的服务.

BluetoothGatt : android.os.DeadObjectException显示日志之前,如下所示

W/bt-smp  (10670): io_cap = 4
W/bt-smp  (10670): new io_cap = 4 p_cb->loc_enc_size = 16
W/BluetoothEventManager( 7380): CachedBluetoothDevice for device 20:73:20:00:6C:B4 not found, calling readPairedDevices().
D/BluetoothAdapterService(1108123608)(10670): Get Bonded Devices being called
E/BluetoothEventManager( 7380): Got bonding state changed for 20:73:20:00:6C:B4, but we have no record of that device.
Run Code Online (Sandbox Code Playgroud)

在发现服务之前连接到BLE设备时的完整日志如下所示

I/BluetoothLeService(10888): BluetoothGattCallback-----newState = 2
I/BluetoothLeService(10888): …
Run Code Online (Sandbox Code Playgroud)

android bluetooth bluetooth-lowenergy android-bluetooth gatt

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

什么是BLE中的"可靠写入"?

在Android的BLE API(BluetoothGatt)中,有一些方法可以处理可靠的写入:

public boolean beginReliableWrite ()

public void abortReliableWrite (BluetoothDevice mDevice)

public boolean executeReliableWrite ()
Run Code Online (Sandbox Code Playgroud)

还有一个Callback(在BluetoothGattCallback中):

public void onReliableWriteCompleted (BluetoothGatt gatt, int status)
Run Code Online (Sandbox Code Playgroud)

我找不到任何相关的文档.它是什么?它与"正常"(不可靠?)的写法有什么不同?

android bluetooth-lowenergy android-bluetooth

13
推荐指数
1
解决办法
9589
查看次数

BLuetooth Gatt Callback无法使用Lollipop的新API

我目前有一种方法可以写入BLE设备发出蜂鸣声.我的蓝牙回拨如下:

ReadCharacteristic rc = new ReadCharacteristic(context, ds.getMacAddress(), serviceUUID, UUID.fromString(myUUID), "") {
                @Override
                public void onRead() {
                    Log.w(TAG, "callDevice onRead");
                    try{Thread.sleep(1000);}catch(InterruptedException ex){}
                    WriteCharacteristic wc = new WriteCharacteristic(activity, context, getMacAddress(), serviceUUID, UUID.fromString(myUUID), ""){
                        @Override
                        public void onWrite(){
                            Log.w(TAG, "callDevice onWrite");
                        }
                        @Override
                        public void onError(){
                            Log.w(TAG, "callDevice onWrite-onError");
                        }
                    };

//                  Store data in writeBuffer
                    wc.writeCharacteristic(writeBuffer);
                }

                @Override
                public void onError(){
                    Log.w(TAG, "callDevice onRead-onError");
                }
            };

            rc.readCharacteristic();
Run Code Online (Sandbox Code Playgroud)

我的ReadCharacteristic实现如下:

public class ReadCharacteristic extends BluetoothGattCallback {
    public ReadCharacteristic(Context context, String macAddress, UUID service, UUID …
Run Code Online (Sandbox Code Playgroud)

android bluetooth-lowenergy android-bluetooth gatt android-5.0-lollipop

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

以编程方式连接到蓝牙

我正在尝试以编程方式将我的设备连接到例如我的耳机上...我有KitKat版本并且所有工作Bluetooth都很完美(总是自动连接没有问题)但是因为我已更新到Lolipop它没有.我想知道是否有任何方法可以将我的任何配对设备连接Android phoneBluetooth打开状态.

从现在开始我已经有了这个代码(获取设备名称和设备地址),因为我想它可以连接做类似device.connect(MAC-Address);但不起作用的东西......

    BluetoothAdapter bluetoothAdapter
    = BluetoothAdapter.getDefaultAdapter();
Set < BluetoothDevice > pairedDevices = bluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
    for (BluetoothDevice device: pairedDevices) {
        mDeviceName.add(device.getName());
        mDeviceMAC.add(device.getAddress());

    }
}
bluetoothClass.setDeviceName(mDeviceName);
bluetoothClass.setDeviceMac(mDeviceMAC);
Run Code Online (Sandbox Code Playgroud)

在我的MotoG(KitKat)上,如果我转动Bluetooth它,它会自动连接到设备(如果它接近并配对...)但是在我的LG G3上我必须转到配置/蓝牙/配对设备/然后点击设备进行连接. ..我想避免这种情况......应该可以吗?


我想知道是否有可能连接到特定的蓝牙只是添加Device nameDevice MAC...或多或少像Android当我点击我的设备连接它自动连接...我只想得到那个CLICK事件.我知道Android应该自动连接到配对的设备,但是有任何异常都没有...唯一的方法是配对它正在点击...这就是为什么我想知道它是否有办法做到这一点......我已经阅读并测试了kcoppock的答案,但它仍然无法正常工作..

有什么建议吗?

编辑

我想要做的主要是自动连接,Bluetooth但是因为我读过嘿你回答...我想出来了,我知道这是一个Android错误,所以我想要做的是选择paired devices和然后单击我要连接的设备(不做任何操作Intent)并连接它,而不是去Configuration/Bluetooth/....顺便说一下,我已经阅读了任何答案StackOverflow …

android android-bluetooth

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

关于连接设备的Android BLE GATT_ERROR(133)

我正在尝试使用MAC地址连接到BLE设备.

BluetoothDevice device = bluetoothAdapter.getRemoteDevice(rememberedDeviceAddress)
bluetoothDevice.connectGatt(context, false, bluetoothGattCallback);
Run Code Online (Sandbox Code Playgroud)

我得到一个回调BluetoothGattCallback.onConnectionStateChangestatus = 133newState = 2 甚至当我的BLE装置被关闭.

newState = 2指的BluetoothProfile.STATE_CONNECTED是我连接到设备并且status = 133是GATT_ERROR(而不是status = 0 SUCCESS)

我没有得到注册回调错误的失败.

设备:一加一(Android 4.4)

任何可能导致此问题的指针都会有所帮助.

注意:问题不会发生在所有设备上.在Android 5.0的Nexus 5上,一切似乎都运行良好

请在下面找到堆栈跟踪:

03-06 13:00:11.994: D/BluetoothGatt(26771): registerApp()
03-06 13:00:11.994: D/BluetoothGatt(26771): registerApp() - UUID='uuid comes here'
03-06 13:00:12.004: D/BluetoothGatt(26771): onClientRegistered() - status=0 clientIf=5
03-06 13:00:42.004: D/BluetoothGatt(26771): onClientConnectionState() - status=133 clientIf=5 device='device id comes here'
Run Code Online (Sandbox Code Playgroud)

android bluetooth bluetooth-lowenergy android-bluetooth

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

三星S6中的音频轨道无法通过蓝牙工作

我正在使用AudioRecord录制音频并使用AudioTrack播放.我正在使用这些类录制Fm模拟音频流并同时传入蓝牙路由器.它成功地与摩托罗拉和三星S5合作.但不与三星Galaxy S6合作.

这是我录制的示例代码

                            audioManager.startBluetoothSco();
                            audioManager.setBluetoothScoOn(true);
                            audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);

                       //FM_SRC is the FM receiving Antenna
                        mRecorder = new AudioRecord(FM_SRC, sampleRateInHz,
                                channelConfigIn,
                                AudioFormat.ENCODING_PCM_16BIT,
                                bufferSizeInBytes);

                        mAudioTrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
                                sampleRateInHz, channelConfigOut,
                                AudioFormat.ENCODING_PCM_16BIT,
                                bufferSizeInBytes,
                                AudioTrack.MODE_STREAM);


                                mRecorder.startRecording();
                                mAudioTrack.play();

                            byte data[] = new byte[bufferSizeInBytes];

                            //Shuffling buffers from record to track object until A2DP routing selected
                            while (mOverA2DP) {
                                // Log.e(TAG, "mOverA2DP:" + mOverA2DP);
                                mRecorder.read(data, 0, bufferSizeInBytes);
                                mAudioTrack.write(data, 0, bufferSizeInBytes);
                            }
Run Code Online (Sandbox Code Playgroud)

请帮我.

android android-audiomanager android-audiorecord android-bluetooth

12
推荐指数
1
解决办法
453
查看次数

如何在我的应用程序中使用智能锁API来解锁模式模式?

我使用的是Android 5.0.该版本提供SmartLock功能,允许通过连接可信设备来解锁密码/模式.我有一个蓝牙低功耗(BLE)设备,注册为可信设备.我想用BLE解锁(模式模式)手机.当BLE和手机连接并且事件可用数据时,它将解锁手机

if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) 
// Calling unlock by the SmartLock API
Run Code Online (Sandbox Code Playgroud)

如果有人使用SmartLock,请给我一些指导吗?我没有找到任何SmartLock API来做到这一点. 在此输入图像描述

android bluetooth bluetooth-lowenergy android-bluetooth google-smartlockpasswords

12
推荐指数
1
解决办法
2990
查看次数

是否可以通过编程方式保持蓝牙遥控快门唤醒?

我有一个与我的Android手机配对的蓝牙遥控快门.遥控快门就像一个蓝牙键盘.

我的应用程序愉快地处理由远程快门创建的键盘事件,但是远程快门在90秒后进入休眠状态,并且需要一键按下才能在发送下一次按键之前将其唤醒.

我需要一种机制来保持遥控快门处于唤醒状态,以便我不会丢失第一次按键.(是的我知道睡眠模式是为了节省能量,但我需要低延迟).

我尝试连接到设备作为客户端和服务器,希望我可以将OutputStream上的内容发送到设备.但我无法创建连接.

有没有办法以编程方式保持遥控快门清醒?

基于以下答案,我怀疑不是

为了完整起见,配对设备的详细信息如下:

  • name = AB Shutter3
  • MAC = Some_MAC_ADDRESS
  • btClass = 2540
  • btType = 1
  • UUID = [00001124-0000-1000-8000-00805f9b34fb,00000000-0000-1000-8000-00805f9b34fb]

android bluetooth android-bluetooth

12
推荐指数
2
解决办法
951
查看次数

通过蓝牙耳机接听电话

我正在开发VoIP-Android-App.我想通过活动中的连接蓝牙耳机接受和拒绝来电.

到目前为止我尝试了什么:

  • 使用媒体会话接收媒体按钮点击.

    问题:如果我们启动BluetoothSCO,我们不会收到任何Media Button点击.如果我们不启动BluetoothSCO,我们会收到Media Button点击,但我们无法区分长按和短按,因为停机时间始终为0,键码始终为KEYCODE_MEDIA_PLAY,ACTION_DOWN后面紧跟ACTION_UP.只有通过蓝牙连接时才会出现这些问题.如果我们通过有线耳机连接,我们会获得相应的密钥代码(KEYCODE_HEADSETHOOK),停机时间不为0.

  • 使用BroadcastReceiver监听蓝牙SCO连接的变化.

    private val scoReceiver = object : BroadcastReceiver() {
        fun onReceive(context: Context, intent: Intent) {
            val state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1)
            val previousState = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_PREVIOUS_STATE, -1)
            if (state == AudioManager.SCO_AUDIO_STATE_DISCONNECTED && previousState == AudioManager.SCO_AUDIO_STATE_CONNECTED) {
                Log.e(TAG, "SCO Disconnected")
                hangupCall()
            }
        }
    }
    
    protected fun onStart() {
        super.onStart()
        val intentFilter = IntentFilter()
        intentFilter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED)
        registerReceiver(scoReceiver, intentFilter)
    }
    
    Run Code Online (Sandbox Code Playgroud)

    通过这种方法,我可以检测用户何时挂断电话,例如长按蓝牙耳机,因为这会触发SCO断开连接.

    问题:我们无法检测用户是否想接听来电.

  • 使用dispatchKeyEvent,onKeyDownonKeyUp.

    问题:他们从来没有被召唤过.

有没有人有任何建议或最佳实践如何正确处理蓝牙耳机?非常感谢任何帮助.提前致谢!

android headset android-bluetooth android-broadcastreceiver voip-android

12
推荐指数
1
解决办法
1030
查看次数

预配对蓝牙设备

我希望能够预先配对蓝牙设备,以便在使用应用程序时节省用户混淆的步骤.我发现有用于预配对蓝牙设备的系统,方法和装置的专利 .

我正在寻找将一系列Android设备与一系列嵌入式设备配对,因此当设置Android设备时,我可以预先将它与需要与之通信的设备配对.我考虑过维护一个可以由应用程序下载和更新的MAC地址列表.

有没有办法配对两个设备而不必让它们接触?

android android-bluetooth pairing

11
推荐指数
1
解决办法
1511
查看次数