博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
蓝牙BLE扫描成功,log中打印出扫描到的设备
阅读量:4610 次
发布时间: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

你可能感兴趣的文章
WordPress优化:为网站添加个性化缩略图标
查看>>
shell脚本分析IP归属地
查看>>
CITRIX XenAPP/TS打印管理ThinPrint.
查看>>
SQL Server以Online模式创建索引
查看>>
微软开放 .NET 框架源代码
查看>>
Jira迁移及内存调整
查看>>
Exchange Server 2010 SP2 新功能简述
查看>>
使用wxWidgets for C++从资源文件中静态装载图像
查看>>
提高数据库安全性的办法
查看>>
工作流编程循序渐进(8:状态机工作流)
查看>>
3.VMware View 4.6安装与部署-connection server(View Standard Server)
查看>>
Lync Server 2013 实战系列之六:标准版-安装和更新LyncServer 系统
查看>>
MariaDB日志审计 帮你揪出内个干坏事儿的小子
查看>>
Reporting Services目录临时数据库文件存在
查看>>
一个Windows Mobile, Windows Embedded CE工程师的找工经历(一)
查看>>
终于有了MSDN上的Blog
查看>>
PHPUnit学习03---使用Mock对象解决测试依赖
查看>>
java类型与Hadoop类型之间的转换
查看>>
允许SQL Server 2005远程连接
查看>>
微软为asp.net ajax和jquery创建了CDN
查看>>