仅扫描设备成功,暂时未在listview中显示,log中已经能打印出
package com.example.suruixue.bletest; import android.app.ListActivity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCallback; import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattDescriptor; import android.bluetooth.BluetoothManager; import android.bluetooth.BluetoothProfile; import android.bluetooth.le.BluetoothLeScanner; import android.bluetooth.le.ScanCallback; import android.bluetooth.le.ScanResult; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.os.ParcelUuid; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.Arrays; public class BleMainActivity extends AppCompatActivity{ //public class BleMainActivity extends ListActivity { private BluetoothAdapter mBluetoothAdapter; private BluetoothDevice mBtDevice; private String TAG = "BleMainActivity"; private BluetoothLeScanner bleScanner; //button private Button mOpenBt; private Button mStartScan; private Button mStopScan; private Button mConnectDevice; //arrarylist private ListView mBleListView; private ArrayList<BluetoothDevice> mLeDevicesList; private LayoutInflater mInflator; private LeDeviceListAdapter mLeListAdapter; // private ListAdapter mLeListAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ble_main); Log.e(TAG,"srx--oncreate >>>"); if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, R.string.ble_supported, Toast.LENGTH_SHORT).show(); } Log.e(TAG,"srx--oncreate end"); //button init mOpenBt = (Button)findViewById(R.id.open_bt); mOpenBt.setOnClickListener(mBtnClickListener); mStartScan = (Button)findViewById(R.id.start_scan); mStartScan.setOnClickListener(mBtnClickListener); mStopScan = (Button)findViewById(R.id.stop_scan); mStopScan.setOnClickListener(mBtnClickListener); mConnectDevice = (Button)findViewById(R.id.connect_device); mConnectDevice.setOnClickListener(mBtnClickListener); //1.initialize bluetooth adapter BluetoothManager bluetoothManager =(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = bluetoothManager.getAdapter(); } @Override protected void onResume() { super.onResume(); mBleListView = (ListView)findViewById(R.id.list_view) ; mLeListAdapter = new LeDeviceListAdapter(); //setListAdapter(mLeListAdapter); mBleListView.setAdapter(mLeListAdapter); //mBleListView.setOnItemClickListener(mItemClickListener); //scanLeDevice(); } private AdapterView.OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } }; private View.OnClickListener mBtnClickListener = new View.OnClickListener() { @Override public void onClick(View v) { Log.e(TAG,"srx--onClickt"); switch (v.getId()){ case R.id.open_bt: Log.e(TAG,"srx--will openbt"); openBt(); // break; case R.id.start_scan: scanBleDevice(true); //1.call scan break; case R.id.stop_scan: scanBleDevice(false); break; /* case R.id.connect_device: connectBleDevice(); //4.may need connect some one bluetoothdevice such as when click this device. break;*/ default: break; } } }; private void openBt(){ //2.open bluetooth Log.e(TAG,"srx-openBt>>"); if((mBluetoothAdapter == null) || !mBluetoothAdapter.isEnabled()){ Log.e(TAG,"srx-openBt-"); Intent enableBleIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBleIntent, 0); } } //3.defination callback private ScanCallback bleScanCallback = new ScanCallback() { @Override public void onScanResult(int callbackType, ScanResult result) { super.onScanResult(callbackType, result); byte[] scanData = result.getScanRecord().getBytes(); //把byte数组转成16进制字符串,方便查看 //Log.e("TAG","onScanResult :"+CYUtils.Bytes2HexString(scanData)); Log.e("TAG", "onScanResult :" + result.getScanRecord().toString());//4.print scanrecord in callback onxxx /*add scaned device to arraylist data source*/ mBtDevice = result.getDevice(); mLeListAdapter.addLeDevice(mBtDevice); mLeListAdapter.notifyDataSetChanged(); } }; private void scanBleDevice(boolean enable){ // mBluetoothAdapter.startLeScan(); //old method bleScanner = mBluetoothAdapter.getBluetoothLeScanner();//1.get blescanner if(enable && (bleScanner!=null)) { bleScanner.startScan(bleScanCallback);//2.call startscan(callback) } else { bleScanner.stopScan(bleScanCallback); } } //connect bluetooth /* private void connectBleDevice(){ BluetoothGatt btGatt = mBtDevice.connectGatt(BleMainActivity.this, false, new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { super.onConnectionStateChange(gatt, status, newState); if(newState == BluetoothProfile.STATE_CONNECTED){ Log.e(TAG,"srx--connected, discover services"); gatt.discoverServices(); //call discoverService } } @Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { super.onCharacteristicRead(gatt, characteristic, status); // String value = UtilOnStr.parseBytesToHexString(characteristic.getValue()); Log.e(TAG,gatt.getDevice().getName() + " srx-read " + characteristic.getValue()); } @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { super.onCharacteristicChanged(gatt, characteristic); Log.e(TAG,gatt.getDevice().getName() + " srx-changed " + characteristic.getValue()); } @Override public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { super.onDescriptorWrite(gatt, descriptor, status); Log.e(TAG,gatt.getDevice().getName() + " srx-write status:" + status); } // discover service,callback @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { super.onServicesDiscovered(gatt, status); Log.e(TAG,gatt.getDevice().getName() + " srx-service discovered status:" + status); } }); //get ble device supported all services/fuction. //btGatt.getServices(); } */ @Override protected void onStop() { super.onStop(); } @Override protected void onDestroy() { super.onDestroy(); } private class LeDeviceListAdapter extends BaseAdapter { private ArrayList<BluetoothDevice> mLeDevices; private LayoutInflater mInflator; public LeDeviceListAdapter() { super(); mLeDevices = new ArrayList<BluetoothDevice>(); mInflator = BleMainActivity.this.getLayoutInflater(); } public void addLeDevice(BluetoothDevice device) { if(!mLeDevices.contains(device)) { mLeDevices.add(device); } } @Override public int getCount() { return 0; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { return null; } } }
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".BleMainActivity"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/open_bt" android:text="Open BT"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/start_scan" android:text="Start Scan"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/stop_scan" android:text="Stop Scan"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/connect_device" android:text="Connect Device"/> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/list_view"> </ListView> </LinearLayout> </android.support.constraint.ConstraintLayout>