zoukankan      html  css  js  c++  java
  • Android BluetoothLeScanner.startScan()方法与传统BluetoothAdapter.startLeScan()方法使用

    一些参考资料链接

    android BLE 扫描BLE设备 BluetoothLeScanner

      http://a1anwang.com/post-37.html

    android BLE Peripheral 手机模拟设备发出BLE广播 BluetoothLeAdvertiser

      http://a1anwang.com/post-36.html

    Android蓝牙开发手册

    https://developer.android.google.cn/guide/topics/connectivity/bluetooth#ManagingAConnection

    Android蓝牙开发API

    https://developer.android.google.cn/reference/android/bluetooth/le/BluetoothLeScanner#startScan(java.util.List<android.bluetooth.le.ScanFilter>,%20android.bluetooth.le.ScanSettings,%20android.app.PendingIntent)

    Android官方sample

    https://developer.android.google.cn/samples/

    Android与蓝牙Ble之间的通信,包括adapter device gatt等知识点

    https://www.cnblogs.com/xxzjyf/p/x_x_z_j_y_f.html#3862764

    https://www.bluetooth.com/specifications/assigned-numbers/service-discovery

    //以前的方式
                BluetoothManager mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    
            BluetoothAdapter mBluetoothAdapter = mBluetoothManager.getAdapter();
    
            mBluetoothAdapter.startLeScan(mLeScanCallback);
    
            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封装了BlutoothDevice
                        mLeDeviceListAdapter.notifyDataSetChanged();
                    }
                });
    
            }
        };
    /*
    LeDeviceListAdapter的私有属性ArrayList<BluetoothDevice> mLeDevices中取得device 
    BluetoothDevice getDevice(int position) {
                return mLeDevices.get(position);
            }
    */
    final BluetoothDevice device = getDevice(int poisition);
    
    /*
            final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);//这是这么个情况,第一个页面已经扫描到了BluetoothDevice,把他的deviceName和Mac地址传入第二个页面,可以直接用这种方式获得一个BlueToothDevice
    */
    
            mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
    
    //现在的方式
    
            BluetoothManager blutoothManager=(BluetoothManager)context.getSystemService(Context.BLUETOOTH_SERVICE);
    
            BluetoothAdapter mBluetoothAdapter=blutoothManager.getAdapter();
    
            BluetoothLeScanner mBluetoothLeScanner=mBluetoothAdapter.getBluetoothLeScanner();
    
    mBluetoothLeScanner.startScan(buildScanFilters(), buildScanSettings(), mScanCallback);//这三个参数需要自己写方法实现,我们重点关注mScanCallback的实现
    
    //在mScanCallback的实现里面,得到的ScanResult封装了BluetoothDevice 
    
     private class SampleScanCallback extends ScanCallback {
    
            @Override
            public void onBatchScanResults(List<ScanResult> results) {
                super.onBatchScanResults(results);
    
                for (ScanResult result : results) {
                    mAdapter.add(result);
                }
                mAdapter.notifyDataSetChanged();
            }
    
            @Override
            public void onScanResult(int callbackType, ScanResult result) {
                super.onScanResult(callbackType, result);
    
                mAdapter.add(result);
                mAdapter.notifyDataSetChanged();
            }
    
            @Override
            public void onScanFailed(int errorCode) {
                super.onScanFailed(errorCode);
                Toast.makeText(getActivity(), "Scan failed with error: " + errorCode, Toast.LENGTH_LONG)
                        .show();
            }
        }
    
    
    
    
    
    
    
    
    
    
                        
  • 相关阅读:
    [原]获取openstack-pike安装包
    [原]Failed connect to mirrors.cloud.aliyuncs.com:80; Connection refused
    [原]shell批量文件增删改前后缀
    [原]CentOS 7 chrony 笔记
    [原]Docker-issue(2) http: server gave HTTP response to HTTPS client
    [原]Docker-issue(1) image name 显示为 <none>
    [原]Django(1)----Django-setting中的STATIC_URL 和STATIC_ROOT 和STATICFILES_DIRS 的区别
    [原]Django-issue(1)---postgresql数据库连接密码错误
    [转]50个极好的bootstrap 后台框架主题下载
    [原]eclipse + pydev :Error in sitecustomize; set PYTHONVERBOSE for tracaeback: KeyError: 'sitecustomize'
  • 原文地址:https://www.cnblogs.com/sunupo/p/10301278.html
Copyright © 2011-2022 走看看