博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
蓝牙BLE扫描成功,log中打印出扫描到的设备
阅读量:4597 次
发布时间:2019-06-09

本文共 9275 字,大约阅读时间需要 30 分钟。

仅扫描设备成功,暂时未在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
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
mLeDevices; private LayoutInflater mInflator; public LeDeviceListAdapter() { super(); mLeDevices = new ArrayList
(); 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; } } }

 

 

转载于:https://www.cnblogs.com/snowdrop/articles/9627919.html

你可能感兴趣的文章
Python复习基础篇
查看>>
关于Cocos2d-x中背景音乐和音效的添加
查看>>
checkbox和文字对齐
查看>>
JConsole远程连接配置 服务器监控工具
查看>>
了解HTTP协议栈(实践篇)
查看>>
loj10035. 「一本通 2.1 练习 1」Power Strings
查看>>
%s的用法
查看>>
调用底层不能直接访问的类和方法
查看>>
清理缓存的方法 #DF
查看>>
JAVA array,map 转 json 字符串
查看>>
2017-12-27练习
查看>>
NET设计规范(二) 命名规范
查看>>
VMware 9.0.1安装Mac OS X Mountain Lion 10.8.2
查看>>
SSL延迟
查看>>
android新手关于左右滑动的问题,布局把<android.support.v4.view.ViewPager/><ImageView/> 放在上面就不行了。...
查看>>
深入理解DIP、IoC、DI以及IoC容器
查看>>
赋值文件
查看>>
Vue 数组 字典 template v-for 的使用
查看>>
蓝牙模块选择经验谈
查看>>
java中==和equals
查看>>