2015-11-02 147 views
2

我有一个BLE设备。设备有一个按钮。目标是在按下设备按钮时在Android设备中触发某些操作。无法发现BLE设备

我的问题是,我能够通过系统蓝牙扫描仪发现并配对我的BLE设备。但是,当我使用BLE扫描内部代码(与谷歌代码相同)时,我无法看到设备。

我有我的以下标签在我的清单。

uses-permission android:name="android.permission.BLUETOOTH" 

uses-permission android:name="android.permission.BLUETOOTH_ADMIN" 

这是我设置的gradle

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(); 
        } 
       }); 
      } 
     }; 
+0

尽量不要配对设备,并检查 –

+0

你可以发布你使用的代码片段吗? – somesh

+0

嗨@somesh 非常标准的代码。我已经提到过它。 – Vegito1044

回答

3

你使用BluetoothAdapter显示设备或者你的服务使用BluetoothGattCallback?

以粗体显示的UUID是否与设备的服务UUID相对应?

+0

是我的设备。 PFA代码片段。我正在使用适配器来显示 – Vegito1044

+0

您的适配器是否设置为您的ListView? – Azartys

0

你有位置设置吗? 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"/> 

完成此操作后,您必须进入手机设置并手动启用位置权限。这是通过设置>隐私和安全>应用权限

不要问我为什么蓝牙需要位置权限。这对我来说也没有意义。事实上,即使位置功能关闭,它仍然需要位置设置。