显示BlueTooth设备列表

时间:2018-02-06 09:56:30

标签: android bluetooth

我正在构建一个应该连接BLE设备的应用。因此,如果我尝试启动我的应用程序,我的智能手机可能会读取其他BlueTooth设备。因此,在这种情况下,我想显示一个包含此设备列表的视图,然后用户可以选择正确的设备。

所以这是我的代码。我怎样才能改变它?

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ble);
        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();
         if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }

        tvBLE = (TextView) findViewById(R.id.tvBLE);
        ibDownload = (ImageButton) findViewById(R.id.ibDownload);               
        ibDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(currDevice!=null){
                    mGatt = currDevice.connectGatt(getBaseContext(), false, gattCallback);
                    scanLeDevice(false);// will stop after first device detection
                }
                LinkedList<SensorData> listaDati =  db.fetchSensorData();
                ListView listView = (ListView)findViewById(R.id.list_view);
                adapter = new SensorDataAdapter(classe, R.layout.sensordata_row, listaDati);
                listView.setAdapter(adapter);                    
            }
        });    

        if (mBluetoothAdapter == null) {
            Toast.makeText(this, "Bluetooth non supportato", Toast.LENGTH_SHORT).show();
            finish();
        }



        for(int i=0; i< 20; i++){
            SensorData mSenData = new SensorData();
            mSenData.setValue(15 + i+"");
            mSenData.setIdType(i);
            mSenData.setValueTimestamp(db.getDateTime());
            db.insertSensorData(mSenData);
        }
        LinkedList<SensorData> listaDati =  db.fetchSensorData();
        ListView listView = (ListView)findViewById(R.id.list_view);
        adapter = new SensorDataAdapter(classe, R.layout.sensordata_row, listaDati);
        listView.setAdapter(adapter);
    }

0 个答案:

没有答案