我在Nexus 7上使用Android 4.4.2.我有一个蓝牙低功耗外设,其服务在重新启动时会发生变化.Android应用程序调用BluetoothGatt.discoverServices().然而,Android只查询外围设备一次以发现服务,后续调用discoverServices()会导致第一次调用的缓存数据,甚至断开连接之间.如果我禁用/启用Android bt适配器,则discoverServices()通过查询外设来刷新缓存.有没有一种程序化的方法来强制Android刷新其'ble服务缓存而不禁用/启用适配器?
我试图通过编程方式获取iPhone/iPod蓝牙的状态,无论是开启还是关闭.是否可以使用某些Apple API或第三方API.
我目前正在开发一个小应用程序,以开始使用蓝牙Android API提供的服务.
编辑 - >答案:
似乎问题是由于特定的Nexus 5设备.好像他们的蓝牙接收器不能正常工作.以下解决方案适用于其他设备
备注:
我在这里阅读了文档:http://developer.android.com/guide/topics/connectivity/bluetooth.html 以及本教程的以下源代码http://www.londatiga.net/it/programming/android/how-to-programmatically-scan-or-discover-android-bluetooth-device /位于/ lorensiuswlt/AndroBluetooth下的github上
我已经完成了几乎所有感兴趣的功能(例如检查适配器是否存在,启用/禁用蓝牙,查询配对的divices,设置适配器是否可发现).
问题:
实际上,当我启动.onDiscovery()方法时,找不到任何设备,即使在我的Nexus 5上的设置/蓝牙中找到了设备.
我是如何处理的:
public class MainActivity extends AppCompatActivity {
private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
...
protected void onCreate(Bundle savedInstanceState) {
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter);
}
Run Code Online (Sandbox Code Playgroud)
该过滤器工作良好,据我可以尝试,即ACTION_STATE_CHANGED(蓝牙启用)和两个ACTION_DISCOVERY _***.
然后成功地调用以下方法:
public void onDiscovery(View view)
{
mBluetoothAdapter.startDiscovery();
}
Run Code Online (Sandbox Code Playgroud)
然后我有我的蓝牙接收器:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) …Run Code Online (Sandbox Code Playgroud) 我是android的新手.我正在设计一个Android应用程序,通过蓝牙从硬件设备接收串行数据.我正在研究Htc desire S.我使用示例蓝牙聊天代码来接收数据.但收到的数据不正确.它错过了一些价值观.任何人都可以请我提供任何其他示例代码,通过蓝牙接收大量数据并将其保存在文件中.
我想基于BlueZ构建ac文件,但在我的系统中似乎没有bluetooth.h文件.
fatal error: bluetooth/bluetooth.h: No such file or directory
Run Code Online (Sandbox Code Playgroud)
我确信蓝牙加密狗运行正常,我已经成功构建了Bluez.
更新
对于我的情况,我在/ user/include/bluetooth文件夹中找到bluetooth.h
package supa.mack.doppler;
import java.util.Set;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.bluetooth.*;
import android.widget.Toast;
public class doppler_test extends Activity {
TextView out;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
out = (TextView) findViewById(R.id.out);
// Getting the Bluetooth adapter
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
out.append("\nAdapter: " + adapter);
// Check for Bluetooth support in the first place
// Emulator doesn't support Bluetooth and will return null …Run Code Online (Sandbox Code Playgroud) 我有一个蓝牙手持式打印机,我能够使用Mac上的SPP连接进行通信(使用Coolterm).当我试图从Android(使用平台7)做同样的事情时,我遇到了多个问题:
打印机似乎不支持/需要PIN身份验证.当从OSX连接时,我刚刚选择了"不要使用引脚"并且它已配对的选项.在Android中,当我使用时device.createRfcommSocketToServiceRecord(),它总是最终要求我输入PIN /密钥(我没有/不需要).我用反射技巧解决了这个问题:
Method m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
BluetoothSocket connection = (BluetoothSocket) m.invoke(device, 1);
Run Code Online (Sandbox Code Playgroud)
我不确定这是否真的有效,但打印机上闪烁的LED指示灯停止闪烁,这让我相信它确实如此.
一旦我有套接字,我尝试使用以下方法将字节数据写入流:
byte[] buffer = new byte[3];
buffer[0] = (byte) 0x8A;
buffer[1] = (byte) 0xC1;
buffer[2] = (byte) 0x04;
outStream.write(buffer);
int response = inStream.read();
mySocket.close();
Run Code Online (Sandbox Code Playgroud)
从OSX上的Coolterm发送相同的三字节序列从打印机打印了一个测试页.但是,这似乎使线程挂在Android上(读取).
我在这里错过了什么吗?
编辑:这似乎只有当我将通道设置为1时才有效.这意味着我在这里做点什么.
我getBluetoothService() called with no BluetoothManagerCallback在Android应用程序中经常出错.
我不知道是什么导致了这个或任何关于蓝牙管理器回调.任何人都可以让我很好地了解可能导致此问题的原因或从哪里开始寻找.
在连接蓝牙设备下面这两种方式有什么区别:
1)
UUID uuid = UUID.fromString(Values.SPP_UUID); //Standard SerialPortService ID
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
Run Code Online (Sandbox Code Playgroud)
2)
Method m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
mmSocket = (BluetoothSocket) m.invoke(mmDevice, 1);
Run Code Online (Sandbox Code Playgroud)
我发现第一种方法不能一直工作,有时它会起作用,但是在关闭蓝牙设备后,它将无法再次工作.第二种方式总是运作良好.我知道它只是打开一个与蓝牙通信的频道,但我不知道如何在不使用uuid的情况下连接到蓝牙设备呢?
如何查看连接的蓝牙设备的电池电量?该设备显示Android上的电池电量,因此我假设该设备支持基于GATT的电池服务.但是,通过在bluetoothctl中输入"menu gatt",然后使用"list-attributes [dev]"列出设备的GATT属性,则不显示任何内容.
一个类似的问题被张贴到SO但OP似乎已经发现,不为我工作的解决方案.当我在bluetoothctl中运行"info [dev]"时,我没有看到电池服务的UUID.
我更喜欢在命令行上运行并且与驱动程序无关的解决方案.
如果这个问题应该发布在SuperUser上,请告诉我.