Android 无法发现可复制设备
我有一个可拆卸的装置。这个设备只有一个按钮。目标是在按下设备按钮时触发android设备中的某些操作 我的问题是,我能够通过系统蓝牙扫描仪发现并与我的BLE设备配对。但当我使用BLE扫描内部代码(和谷歌代码相同)时,我看不到设备 我的清单中有以下标签。Android 无法发现可复制设备,android,mobile,android-studio,bluetooth,bluetooth-lowenergy,Android,Mobile,Android Studio,Bluetooth,Bluetooth Lowenergy,我有一个可拆卸的装置。这个设备只有一个按钮。目标是在按下设备按钮时触发android设备中的某些操作 我的问题是,我能够通过系统蓝牙扫描仪发现并与我的BLE设备配对。但当我使用BLE扫描内部代码(和谷歌代码相同)时,我看不到设备 我的清单中有以下标签。 uses-permission android:name="android.permission.BLUETOOTH" uses-permission android:name="android.permission.BLUETOOTH_ADM
uses-permission android:name="android.permission.BLUETOOTH"
uses-permission android:name="android.permission.BLUETOOTH_ADMIN"
这是我的渐变设置
minSdkVersion 18, targetSdkVersion 22
Phone- Nexus 5 | Android M
这是日志。正如您在粗体文本中看到的,它会发现设备,但不会添加它知道为什么会发生这种情况吗?
BtGatt.GattService﹕ registerClient() - UUID=b7516aaa-22b1-4d8f-a71e-405e5584edcf BtGatt.GattService﹕ onClientRegistered() - UUID=b7516aaa-22b1-4d8f-a71e-405e5584edcf, clientIf=5
BtGatt.GattService﹕ start scan with filters
BtGatt.ScanManager﹕ handling starting scan
BtGatt.ScanManager﹕ configureRegularScanParams() - queue=1
BtGatt.ScanManager﹕ configureRegularScanParams() - ScanSetting Scan mode=2 mLastConfiguredScanSetting=-2147483648
BtGatt.ScanManager﹕ configureRegularScanParams - scanInterval = 8000configureRegularScanParams - scanWindow = 8000
BtGatt.GattService﹕ onScanParamSetupCompleted : 0
bt_btif_gattc﹕ btif_gattc_update_properties BLE device name=Security Tag len=12 dev_type=2 bt_btif_gattc﹕ btif_gattc_update_properties BLE device name=BlueFind len=15 dev_type=2
BtGatt.GattService﹕ stopScan() - queue size =1
BtGatt.ScanManager﹕ stop scan
BtGatt.ScanManager﹕ configureRegularScanParams() - queue=0
BtGatt.ScanManager﹕ configureRegularScanParams() - ScanSetting Scan mode=-2147483648 mLastConfiguredScanSetting=2
BtGatt.ScanManager﹕ configureRegularScanParams() - queue emtpy, scan stopped
BtGatt.GattService﹕ unregisterClient() - clientIf=5
BtGatt.GattService﹕ registerClient() -
**UUID=2f2450e9-ea7a-4dfe-aef2-27bcd75c83c5**
BtGatt.GattService﹕ onClientRegistered() - UUID=2f2450e9-ea7a-4dfe-aef2-27bcd75c83c5, clientIf=5
BtGatt.GattService﹕ start scan with filters
BtGatt.ScanManager﹕ handling starting scan BtGatt.ScanManager﹕ configureRegularScanParams() - queue=1
BtGatt.ScanManager﹕ configureRegularScanParams() - ScanSetting Scan mode=2 mLastConfiguredScanSetting=-2147483648
BtGatt.ScanManager﹕ configureRegularScanParams - scanInterval = 8000configureRegularScanParams - scanWindow = 8000
BtGatt.GattService﹕ onScanParamSetupCompleted : 0
**bt_btif_gattc﹕ btif_gattc_update_properties BLE device name=Security Tag len=12 dev_type=2**
**bt_btif_gattc﹕ btif_gattc_update_properties BLE device name=BlueFind len=15 dev_type=2**
BtGatt.GattService﹕ stopScan() - queue size =1
BtGatt.ScanManager﹕ stop scan
BtGatt.ScanManager﹕ configureRegularScanParams() - queue=0
BtGatt.ScanManager﹕ configureRegularScanParams() - ScanSetting Scan mode=-2147483648 mLastConfiguredScanSetting=2
BtGatt.ScanManager﹕ configureRegularScanParams() - queue emtpy, scan stopped
BtGatt.GattService﹕ unregisterClient() - clientIf=5
--EDIT1--
这是我的代码片段
private void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
invalidateOptionsMenu();
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
invalidateOptionsMenu();
}
// Adapter for holding devices found through scanning.
private class LeDeviceListAdapter extends BaseAdapter {
private ArrayList<BluetoothDevice> mLeDevices;
private LayoutInflater mInflator;
public LeDeviceListAdapter() {
super();
mLeDevices = new ArrayList<BluetoothDevice>();
mInflator = BLEScanActivity.this.getLayoutInflater();
}
public void addDevice(BluetoothDevice device) {
if(!mLeDevices.contains(device)) {
mLeDevices.add(device);
}
}
public BluetoothDevice getDevice(int position) {
return mLeDevices.get(position);
}
public void clear() {
mLeDevices.clear();
}
@Override
public int getCount() {
return mLeDevices.size();
}
@Override
public Object getItem(int i) {
return mLeDevices.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
// General ListView optimization code.
if (view == null) {
view = mInflator.inflate(R.layout.listitem_device, null);
viewHolder = new ViewHolder();
viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
final BluetoothDevice device = mLeDevices.get(i);
final String deviceName = device.getName();
if (deviceName != null && deviceName.length() > 0)
viewHolder.deviceName.setText(deviceName);
else
viewHolder.deviceName.setText(R.string.unknown_device);
viewHolder.deviceAddress.setText(device.getAddress());
return view;
}
}
// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
}
};
private void扫描设备(最终布尔启用){
如果(启用){
//在预定义的扫描周期后停止扫描。
mHandler.postDelayed(新的Runnable(){
@凌驾
公开募捐{
mScanning=false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
无效操作菜单();
}
},扫描周期);
mScanning=true;
mBluetoothAdapter.startedscan(mLeScanCallback);
}否则{
mScanning=false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
无效操作菜单();
}
//用于保存通过扫描找到的设备的适配器。
私有类LeDeviceListAdapter扩展BaseAdapter{
专用阵列列表设备;
私人公寓;
公共LeDeviceListAdapter(){
超级();
mLeDevices=新的ArrayList();
mInflator=BLEScanActivity.this.getLayoutFlater();
}
公用设备(蓝牙设备){
如果(!mLeDevices.contains(设备)){
mLeDevices.add(设备);
}
}
公共蓝牙设备getDevice(内部位置){
返回设备。获取(位置);
}
公共空间清除(){
mle.clear();
}
@凌驾
public int getCount(){
返回mLeDevices.size();
}
@凌驾
公共对象getItem(int i){
返回设备。获取(i);
}
@凌驾
公共长getItemId(int i){
返回i;
}
@凌驾
公共视图getView(int i、视图视图、视图组视图组){
持票人持票人;
//通用ListView优化代码。
如果(视图==null){
view=mInflator.充气(R.layout.listitem\u设备,空);
viewHolder=新的viewHolder();
viewHolder.deviceAddress=(TextView)view.findViewById(R.id.device\u地址);
viewHolder.deviceName=(TextView)view.findViewById(R.id.device\u name);
view.setTag(viewHolder);
}否则{
viewHolder=(viewHolder)view.getTag();
}
最终BluetoothDevice=mLeDevices.get(i);
最后一个字符串deviceName=device.getName();
如果(deviceName!=null&&deviceName.length()>0)
viewHolder.deviceName.setText(deviceName);
其他的
viewHolder.deviceName.setText(R.string.unknown_设备);
viewHolder.deviceAddress.setText(device.getAddress());
返回视图;
}
}
//设备扫描回调。
私有BluetoothAdapter.LeScanCallback mLeScanCallback=
新的BluetoothAdapter.LeScanCallback(){
@凌驾
public void onLeScan(最终BluetoothDevice设备,int rssi,字节[]扫描记录){
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
mLeDeviceListAdapter.addDevice(设备);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
}
};
您是使用BluetoothAdapter来显示设备,还是在服务中使用BluetoothGattCallback
粗体的UUID是否与设备的服务UUID相对应?上是否有位置设置?Android 6.0+需要位置设置。您应该向清单中添加:
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
完成后,您必须进入手机设置并手动启用位置权限。这是通过设置>隐私和安全>应用程序权限来完成的
不要问我为什么蓝牙需要位置权限。这对我来说也没有意义。事实上,即使位置功能已关闭,它仍然需要位置设置。请尝试不要将设备配对,然后检查您是否可以发布您正在使用的代码段?Hi@somesh非常标准的代码。“我已经提过了。”马特拉齐06我已经试过了。也使用了多部手机,没有成功。我遇到了类似的问题:。我注意到您正在使用旧的(21世纪以前的BLE API来发现设备)。我尝试了新的API(使用getLeScanner()),但它仍然不适合我。你能解决它吗?我还可以在你的日志中看到一些设备被找到,但回调没有被触发。是吗?是的,那是我的设备。PFA代码片段。我正在使用适配器显示您的适配器是否设置为ListView?