问题标签 [android-bluetooth]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
5193 浏览

android - 对蓝牙使用 ACL_CONNECTED 和 ACL_DISCONNECTED 似乎不适用于 Android 4.2.2。任何解决方案?

一段时间以来,我一直在使用接收器来监控这些意图,以监控活动的蓝牙连接。我现在发现在运行 android 4.2.2 的 Galaxy S4 上,这些意图似乎根本不需要。

它适用于我的 Transformer Infinity(4.1)、Droid Bionic(4.1) 和 Droid X(2.3.3)。4.2.2 中有什么改变破坏了这种方法吗?

这是我的代码供参考。

0 投票
1 回答
95 浏览

android - Android BlutoothChat,使类实例在其他活动中可用

我正在搞乱其中一个 Android Samples 蓝牙聊天,我对 Android 比较陌生,所以我来这里是为了获得用户的意见,在主活动类和类“BluetoothChatService mChatService”的实例中创建了所有控制关于蓝牙连接,现在我创建了一个启动按钮页面的新活动,这些按钮将发送某些硬编码消息,具体取决于按下哪个按钮,因为“mChatService”已启动并正在处理我想要的连接使这个类实例在我新创建的 Activity 中可用,这样我就可以立即发送消息,

使它可用的最佳实践是什么?,我已经阅读了关于序列化类(在这种情况下不起作用),所以我可以将它与 Intent 以及单例一起传递?

谁能建议这应该怎么做?

谢谢!

0 投票
2 回答
7130 浏览

android - 自动查找已配对的蓝牙设备,当它们在范围内时

我不是蓝牙专家,我想知道当它们在彼此的范围内时,有哪些可能性可以自动找到已经配对的蓝牙设备。

背景:在我们的案例中,Android 应用程序需要通过蓝牙 (Rfcomm) 连接到专用配件。两个设备是彼此已知的(它们是配对的)。Android 应用程序注册一个广播接收器。在应用程序启动期间,应用程序启动发现以查找专用附件。如果配件在范围内,一切都很好。

问题:用户在专用附件范围之外启动应用程序。Android 应用程序尝试发现附件但没有成功。然后用户进入蓝牙附件的范围。广播接收器不会收到关于现在范围内的附件的通知。

类似的线程/可能的解决方案 在 stackoverflow 上已经提出了类似的问题(例如,在范围内时自动连接到蓝牙设备)。但不断尝试发现范围内的蓝牙设备并不是我想要的,因为这会导致 Android 设备的电池消耗过多。

另一种解决方案是尝试在 Activity 的 onResume 方法中连接到配对设备。这可行,但缺点是应用程序无法在后台运行。因此,用户必须至少将应用程序带到前台一次才能启动连接。

我想到的第三个想法是在 Android 应用程序中也实现一个服务器套接字。当 android 应用程序启动并且发现没有成功完成时,Android 应用程序可以创建服务器套接字并监听附件的传入通知。这在某些情况下会有所帮助(例如,用户启动他的应用程序,接近附件,激活附件并且附件在启动时通知应用程序它现在在范围内)。但这仍然不是 100% 的解决方案,因为两个设备都可以在彼此的范围之外启动。此外,还必须实现附加功能(Android 设备中的蓝牙服务器套接字……)。

所以我想知道是否存在更好的解决方案。我正在寻找一种不需要额外的 ServerSockets 的解决方案,并且我总是收到两个已配对的设备在彼此范围内的通知:-)

谢谢你的帮助!

0 投票
3 回答
30656 浏览

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

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

如果我按下耳机上的按钮,“日志猫”会显示以下内容:

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

总结不起作用答案: 音量按钮必须由“VOLUME_CHANGED_ACTION”捕获。问题是这个意图被广播到其他应用程序并且 abortBroadcast() 不起作用(它仅适用于“有序”广播)。另一个问题是有线耳机和电话上的按键触发 onReceive() 两次(为什么?)蓝牙耳机触发一次。下一个问题是蓝牙耳机的第三把钥匙。它触发语音命令(s-voice 在 s3 上开始),我试图捕捉许多不同的意图,但我无法“接收”这个按钮按下并且不知道为什么。最后,我想捕获各种按钮,并且不希望它们被其他应用程序处理(例如使用 onKeyDown 并返回 true)。

0 投票
1 回答
2813 浏览

android - 从 android JB 4.1.1 中的命令行进行蓝牙配对

在我的参考板上,我使用的是 Android Jelly Bean 4.1.1。我想从命令行使用蓝牙。我知道启用/禁用蓝牙、扫描设备等命令,但我不知道如何从命令行与扫描的设备配对。Android Jelly Bean 4.1.1 正在为蓝牙使用 Bluez 堆栈。谁能帮助我,这样我就可以从命令行将我的板与其他蓝牙设备配对。谢谢。

0 投票
0 回答
379 浏览

android - 如何绕过 LG nexus 4 (E960) 中的蓝牙配对对话框?

我正在使用以下代码在我的应用程序中将嵌入式蓝牙设备与 LG nexus 4 连接

它在三星 Galaxy Grand 等其他设备上运行良好,但是当我尝试使用 LG nexus 4 时,它会显示一个对话框,其中包含两个选项对或取消,我不希望它直接与设备连接。请帮我。

0 投票
5 回答
100476 浏览

android - Android 4.3 Bluetooth Low Energy unstable

I am currently developing an application that will use Bluetooth Low Energy (testing on the Nexus 4). After getting started with the official BLE APIs in Android 4.3, I have noticed that after I connect a device for the first time I am rarely able to successfully connect to / communicate with that device or any other device again.

Following the guide here, I can successfully connect to a device, scan services and characteristics, and read/write/receive notifications without any issues. However, after disconnecting and re-connecting, I am often unable to either scan services/characteristics or unable to complete a read/write. I can't find anything in the logs to indicate why this is happening.

Once this happens I have to uninstall the application, disable Bluetooth, and restart the phone before it will start working again.

Whenever a device is disconnected I make sure to call close() on the BluetoothGatt object and set it to null. Any insights?


EDIT:
Log dumps: For these logs I rooted my phone and upped the trace levels of related items in /etc/bluetooth/bt_stack.conf

Successful connection - First attempt after rebooting the phone and installing the app. I am able to connect, discover all services/characteristics, and read/write.

Failed Attempt 1 - This is the next attempt after disconnecting from the successful connection above. It seems I was able to discover characteristics, but the first attempt to read returned a null value and disconnected soon thereafter.

Failed Attempt 2 - An example where I am not even able to discover services/characteristics.


EDIT 2:
The device to which I am trying to connect is based on TI's CC2541 chip. I obtained a TI SensorTag (also based on the CC2541) to play around with and discovered that TI released an android app for the SensorTag yesterday. However, this app has the same problem. I tested this on two other Nexus 4s with the same result: Connection to the SensorTag is successful the first or second time, but (according to the logs) fails to discover services thereafter, causing all sorts of crashes. I'm starting to wonder if it's an issue with this specific chip?

0 投票
1 回答
5764 浏览

android - 通过蓝牙发送一个整数并接收一个字符串

我有我想要进行变量传输的蓝牙设备地址(我已经使用 BluetoothAdapter 来发现设备和......)

现在我需要一个简单的示例来使用套接字发送和接收变量......(我需要使用 BluetoothSocket 对吗?)

我看过蓝牙聊天示例,但我认为其中的许多代码对我来说都很不寻常,所以我想请人帮助我提供一个简单的示例代码,以便轻松发送整数和接收字符串,请。

我之前检查过蓝牙支持并找到设备的蓝牙地址。

我有这样的东西:

有人能帮助我吗 ?


已解决:我使用了这个简单的教程: http: //manojprasaddevelopers.blogspot.com/2012/02/bluetooth-data-transfer-example.html

由于声誉低下,我要到接下来的几个小时才能回答我的问题,所以我编辑了我的帖子。无论如何,对不起,如果这不是一个好问题,请。:)


编辑 2:毕竟,也使用了 BluetoothChat 示例...... :)

0 投票
1 回答
1428 浏览

android - 从蓝牙低功耗 GATT 特性中检索大的 32 位无符号整数

我正在尝试使用我的实例2^32 - 1的方法从蓝牙 4.0 特性中检索一个非常大(可能)的无符号 32 位整数。getIntValueBluetoothGattCharacteristic

我正在传递FORMAT_UINT32getIntValue,但因为它返回一个Integer它不能返回一个大于的数字2^31 - 1。当我尝试获取一个大于2^31 - 1它的值时,它会错误地返回一个负整数。

如果它不可能返回一个无符号的 32 位整数,那么getIntValue接受它有什么意义呢?FORMAT_UINT32有什么我想念的吗?

我唯一的选择是使用getValue()并手动将其转换为long?

0 投票
0 回答
723 浏览

android - 无法通过蓝牙连接两台设备进行聊天应用程序

我正在尝试实现蓝牙聊天应用程序。该应用程序显示已配对设备的列表,您可以从中选择一个设备,该应用程序将重定向到 ChatActivity。启动此活动时,应用程序应搜索所选设备并连接到它。

当我在两台设备上安装 apk 并尝试将它们连接在一起时,它不起作用。这些设备是可发现的并且它们在连接范围内。该应用程序无法在两个设备之间建立套接字连接。ChatService 代码取自 sdk 中的示例。

我的代码是:

BTChatService.java

聊天活动.java

@SuppressLint("HandlerLeak") 公共类 ChatActivity 扩展 ListActivity {

}