Ble扫描设备Android

时间:2017-03-05 20:00:25

标签: java android bluetooth-lowenergy

我正在扫描BLE设备。 这是我的代码;

public class MainActivity extends AppCompatActivity {



private boolean mScanning;Button ara ;
ListView lw;
private BluetoothAdapter mBluetoothAdapter;
private int REQUEST_ENABLE_BT = 1;
private Handler mHandler;
private static final long SCAN_PERIOD = 10000;
private BluetoothLeScanner mLEScanner;
private ScanSettings settings;
private List<ScanFilter> filters;
private BluetoothGatt mGatt;
private ArrayList<String> deviceListesi = new ArrayList<String>();
private ArrayList<String> mDeviceStore = new ArrayList<String>();
private ArrayAdapter<String> deviceAdapter ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mHandler = new Handler();
    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        Toast.makeText(this, "BLE Not Supported",
                Toast.LENGTH_SHORT).show();
        finish();
    }
    final BluetoothManager bluetoothManager =
            (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();
    lw = (ListView) findViewById(R.id.listview);
    ara = (Button) findViewById(R.id.button2);
    deviceAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, deviceListesi);
    mBluetoothAdapter.startDiscovery();
    ara.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            scanLeDevice(true);
        }
    });
}
private final BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
    @Override
    public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {
        System.out.println("Buradayım mmmmmmmmmmmm");
        final String deviceIsim = device.getName()+" "+device.getAddress();
        Toast.makeText(getApplicationContext(),deviceIsim,Toast.LENGTH_SHORT).show();
        mDeviceStore.add(deviceIsim);


        for (final String leDevice : mDeviceStore) {
                deviceListesi.add(leDevice);

        }

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
               lw.setAdapter(deviceAdapter);

            }
        });
    }
};
public void scanLeDevice( final boolean enable) {
    if (enable) {
        if (mScanning) {
            return;
        }
        Log.d("TAG", "~ Starting Scan");
        // Stops scanning after a pre-defined scan period.

            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Log.d("TAG", "~ Stopping Scan (timeout)");
                    mScanning = false;
                    mBluetoothAdapter.stopLeScan(mLeScanCallback);
                }
            }, SCAN_PERIOD);

        mScanning = true;
        mBluetoothAdapter.startLeScan(mLeScanCallback);
    } else {
        Log.d("TAG", "~ Stopping Scan");
        mScanning = false;
        mBluetoothAdapter.stopLeScan(mLeScanCallback);
    }
}

}

我有列表视图,但没有发生什么?这是android监视器打印;

  

D / TAG:〜开始扫描D / TAG:〜停止扫描(超时)   D / BluetoothAdapter:stopLeScan()D / BluetoothAdapter:扫描未启动   但

我错在哪里?

1 个答案:

答案 0 :(得分:0)

删除行

mBluetoothAdapter.startDiscovery();

Bluetooth LE和&#34; old&#34;蓝牙是不同的东西。你不能混合这两者。