ESP32S蓝牙06
前一篇的经典蓝牙代码编程仅仅是开胃小菜,我们的重点是BLE低功耗蓝牙。我们先来完成BLE的第一个小任务:搜索BLE蓝牙设备。
我们开始先上代码吧:这个是程序的代码 MainActivity. java
package com.example.bluetooth;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.Manifest;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothManager;
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.os.Build;
import android.telecom.Connection;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ListAdapter;
import java.util.ArrayList;
import java.util.List;
@SuppressLint("NewApi")
public class MainActivity extends AppCompatActivity {
private Button scan_button ;
private TextView showresult;
private Button connect_button;
private BluetoothAdapter bleadapter;
private BluetoothGatt bluetoothGatt;
private BluetoothLeScanner blescanner;
private ScanCallback scanCallback;
private BluetoothDevice bledevice;
private boolean isfindmyudevice=false;
private String device_name;
private String device_adress;
private List
private ArrayList list;
private ArrayAdapter adapter;
private ListView mListView;
private boolean mScanning = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showresult = (TextView) findViewById(R.id.txt_msg);
scan_button = (Button) findViewById(R.id.btn_scan);
connect_button = (Button) findViewById(R.id.btn_connect);
mListView = (ListView) findViewById(R.id.listView1);
//初始化ble设配器
BluetoothManager manager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
bleadapter = manager.getAdapter();
//判断蓝牙是否开启,若是关闭则请求打开蓝牙
if (bleadapter == null || !bleadapter.isEnabled()) {
//方式一:请求打开蓝牙
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, 1);
}
//搜索按钮
scan_button.setonClickListener(new View.onClickListener() {
@Override
public void onClick(View v) {
showresult.setText("star scan");
//初始化列表变量,一个用于存储自定义类,一个用于存储字符串
mDeviceList=new ArrayList
list = new ArrayList();
//把扫描过程放进一个线程里面进行
new Thread(new Runnable() {
@Override
public void run() {
//如果发现一个BLE设备,就会执行一次callback回调函数
bleadapter.startLeScan(callback);
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
bleadapter.stopLeScan(callback);
showresult.setText("stop scan");
}
}).start();
}
});
connect_button.setonClickListener(new View.onClickListener() {
@Override
public void onClick(View v) {
}
});
//当用户点击选择列表框的时候
mListView.setonItemClickListener(new AdapterView.onItemClickListener() {
BluetoothDevice mDevice;
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
//获取用户选中的蓝牙设备的MAC字符串,并从MAC获得设备对象
device_adress=mDeviceList.get(position).getMac();
bledevice = bleadapter.getRemoteDevice(device_adress);
showresult.setText("selected: " + device_adress);
}
});
}
//这个是扫描的回调函数,每当扫描到一个BLE设备时,就会运行一次这个函数
public BluetoothAdapter.LeScanCallback callback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice bluetoothDevice, int i, byte[] bytes) {
if (bluetoothDevice != null){
//这里给大家另外两种显示扫描结果的方法,可以用消息框或标签来显示
//Toast.makeText(MainActivity.this, bluetoothDevice.getName(), Toast.LENGTH_SHORT).show();
//showresult.append(bleDevice.getName() + " " + bleDevice.getMac() + "n");
BLEDevice bleDevice = new BLEDevice(bluetoothDevice.getName(),bluetoothDevice.getAddress());
if(!mDeviceList.contains(bleDevice)){
//存入自定义类BLEDevice的集合中
mDeviceList.add(bleDevice);
//存入字符串集合中,并显示在列表框中
list.add(bleDevice.getName() + " " + bleDevice.getMac());
adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, list);
mListView.setAdapter(adapter);
}
}
}
};
//这个是自定义类,用于保存搜索到的BLE设备的名字和MAC地址
public class BLEDevice {
private String name;
private String mac;
public BLEDevice(String name, String mac) {
this.name = name;
this.mac = mac;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMac() {
return mac;
}
public void setMac(String mac) {
this.mac = mac;
}
@Override
public boolean equals(Object o) {
return !TextUtils.isEmpty(this.mac) &&!TextUtils.isEmpty(((BLEDevice)o).mac)
&&this.mac.equals(((BLEDevice) o).getMac());
}
}
}
这个是程序的主界面: activity_main.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
android:id="@+id/listView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="visible" />



