Android Bluetoothスキャン、ペアリング
16301 ワード
Android開発でBluetoothを使うのは、一般的には外付けの接続ですが、プリンタを接続するなど、携帯電話や携帯電話の使用に必要な機能があればOKです
権限の使用
スキャン
権限の使用
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-feature android:name="android.hardware.location.gps"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
スキャン
private ListView lv_device;
private Button bt_scan;
private BluetoothAdapter bluetoothAdapter;
private BluetoothManager bluetoothManager;
private final int requestCode = 0;
private ArrayList mList = new ArrayList<>();
private boolean isScanleFlag = false;//
private final long SCANTIME = 1000*60;//
private BluetoothAdapter.LeScanCallback callback;//
private boolean isHasDevice = false;//
private boolean isConnectDevice = false;//
private BluetoothGattCallback bluetoothGattCallback;//
private DeviceAdapter adapter;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
addListenner();
initBlue();
scanLeDevice(true);
}
private void initView() {
lv_device = (ListView) findViewById(R.id.lv_device);
bt_scan = (Button) findViewById(R.id.bt_scan);
adapter = new DeviceAdapter(this, mList);
lv_device.setAdapter(adapter);
}
private void initBlue() {
bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter();
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()){//
startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), requestCode);
}else {//
Set devices = bluetoothAdapter.getBondedDevices();
for (final BluetoothDevice mDevice: devices) {
mList.add(mDevice);
mDevice.connectGatt(this, false, bluetoothGattCallback);
}
}
}
private void addListenner() {
/** ***/
bt_scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isScanleFlag){
scanLeDevice(!isScanleFlag);
}
}
});
/*** ***/
callback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
for (int i = 0; i < mList.size(); i++) {
if (mList.get(i).getAddress().equals(device.getAddress())){
isHasDevice = true;
}
}
if (!isHasDevice && device.getName() !=null){
mList.add(device);
String name = device.getAddress();
adapter.notifyDataSetChanged(mList);
Log.e("device", name + "-------" + device.getAddress());
}
isHasDevice = false;
}
};
lv_device.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
mList.get(position).connectGatt(MainActivity.this, false, bluetoothGattCallback);
}
});
/*** ***/
bluetoothGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
if (status != BluetoothGatt.GATT_SUCCESS){
isConnectDevice = false;
Log.e("device", status + "-----onConnectionStateChange-- ");
gatt.close();
return;
}
if (newState == BluetoothGatt.STATE_CONNECTED){
Log.e("device", "-----onConnectionStateChange-- ");
isConnectDevice = true;
gatt.discoverServices();
}else if (newState == BluetoothGatt.STATE_DISCONNECTED){
Log.e("device", "-----onConnectionStateChange-- ");
isConnectDevice = false;
gatt.close();
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
Log.e("device", "-----onServicesDiscovered-- ");
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
Log.e("device", "-----onCharacteristicRead--");
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
Log.e("device", "-----onCharacteristicWrite--");
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
Log.e("device", "-----onCharacteristicChanged--");
}
@Override
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorRead(gatt, descriptor, status);
Log.e("device", "-----onDescriptorRead--");
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
Log.e("device", "-----onDescriptorWrite--");
}
@Override
public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
super.onReliableWriteCompleted(gatt, status);
Log.e("device", "-----onReliableWriteCompleted--");
}
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
super.onReadRemoteRssi(gatt, rssi, status);
Log.e("device", "-----onReadRemoteRssi--");
}
@Override
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
super.onMtuChanged(gatt, mtu, status);
Log.e("device", "-----onMtuChanged--");
}
};
}
/***
*
* @param enable
*/
private void scanLeDevice(boolean enable){
if (enable){
handler.postDelayed(new Runnable() {
@Override
public void run() {
isScanleFlag = false;
bluetoothAdapter.stopLeScan(callback);
Log.e("device", "-------stop");
}
}, SCANTIME);
isScanleFlag = true;
bluetoothAdapter.startLeScan(callback);
Log.e("device", "-------start");
}else {
isScanleFlag = false;
bluetoothAdapter.stopLeScan(callback);
}
}