首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法发现BLE设备

无法发现BLE设备
EN

Stack Overflow用户
提问于 2015-11-02 13:57:41
回答 2查看 1.3K关注 0票数 2

我有个BLE装置。设备只有一个按钮。目标是在android设备中按下设备按钮时触发某些动作。

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

我在我的舱单上有以下标签.

代码语言:javascript
运行
复制
uses-permission android:name="android.permission.BLUETOOTH"

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

这是我的等级设置

代码语言:javascript
运行
复制
minSdkVersion 18, targetSdkVersion 22

Phone- Nexus 5 | Android M

这是原木。正如您在粗体文本中所看到的,它发现了设备,但没有添加它。知道为什么会这样吗?

代码语言:javascript
运行
复制
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 1--

这是我的代码片段

代码语言:javascript
运行
复制
   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();
                    }
                });
            }
        };
EN

回答 2

Stack Overflow用户

发布于 2015-11-02 14:21:58

您是使用BluetoothAdapter来显示设备,还是在服务中使用BluetoothGattCallback?

UUID是否与设备的服务UUID对应?

票数 3
EN

Stack Overflow用户

发布于 2016-05-27 16:24:06

你有位置设置吗?Android 6.0+需要位置设置。你应该在你的清单上加上:

代码语言:javascript
运行
复制
<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"/>

完成此操作后,必须进入您的电话设置并手动启用位置权限。这是通过Settings>Privacy和Safety>App权限完成的。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33479314

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档