package com.bw.day6_bluetooth;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
BluetoothManager bluetoothManager;
BluetoothAdapter bluetoothAdapter;
List<BluetoothDevice> list=new ArrayList<>();
List<BluetoothDevice> list2=new ArrayList<>();
MyBaseAdapter adapter;
MyBaseAdapter2 adapter2;
private ListView lv,lv2;
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case 121:
// 解决oppo机型兼容问题:msg's executing time is too long
bluetoothAdapter.disable();
break;
}
}
};
List<String> mPermissionList = new ArrayList<>();
List<String> mPermissionList2 = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
lv2 = (ListView) findViewById(R.id.lv2);
//动态权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
requestPermissions(new String[]{
Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.ACCESS_NETWORK_STATE,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
},101);
}
// todo 蓝牙动态申请权限
// if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S){
// // Android 版本大于等于 Android12 时
// // 只包括蓝牙这部分的权限,其余的需要什么权限自己添加
// mPermissionList.add(Manifest.permission.BLUETOOTH);
// mPermissionList.add(Manifest.permission.BLUETOOTH_ADMIN);
// mPermissionList.add(Manifest.permission.BLUETOOTH_SCAN);
// mPermissionList.add(Manifest.permission.BLUETOOTH_ADVERTISE);
// mPermissionList.add(Manifest.permission.BLUETOOTH_CONNECT);
// } else {
// // Android 版本小于 Android12 及以下版本
// mPermissionList.add(Manifest.permission.ACCESS_COARSE_LOCATION);
// mPermissionList.add(Manifest.permission.ACCESS_FINE_LOCATION);
// }
//
// if(mPermissionList.size() > 0){
// ActivityCompat.requestPermissions(MainActivity.this,mPermissionList.toArray(new String[0]),1001);
// }
adapter=new MyBaseAdapter(MainActivity.this,list);
lv.setAdapter(adapter);
adapter2=new MyBaseAdapter2(MainActivity.this,list2);
lv2.setAdapter(adapter2);
bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter();
BluetoothReciver bluetoothReciver = new BluetoothReciver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
registerReceiver(bluetoothReciver,intentFilter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
list.get(i).createBond();
}
});
}
public void offclick(View view) {
Intent intent = new Intent();
intent.setAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
intent.setAction(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(intent);
}
public void yitian(View view){
Set<BluetoothDevice> bluetoothDevices=bluetoothAdapter.getBondedDevices();
for (BluetoothDevice bluetoothDevice : bluetoothDevices) {
list2.add(bluetoothDevice);
adapter2.notifyDataSetChanged();
}
}
public void onclick(View view) {
if (bluetoothAdapter.isEnabled()) {
handler.sendEmptyMessage(121);
} else {
Toast.makeText(this, "蓝牙已经关闭", Toast.LENGTH_SHORT).show();
}
}
public void disbul(View view) {
bluetoothAdapter.startDiscovery();
}
public class BluetoothReciver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//远程设备
BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
list.add(bluetoothDevice);
adapter.notifyDataSetChanged();
String name = bluetoothDevice.getName();
String address = bluetoothDevice.getAddress();
Log.i("Bluetooth",name+address);
}
}
}
适配器
package com.bw.day6_bluetooth;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.pm.LabeledIntent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
public class MyBaseAdapter extends BaseAdapter {
Context context;
List<BluetoothDevice> list;
public MyBaseAdapter(Context context, List<BluetoothDevice> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View inflate = LayoutInflater.from(context).inflate(R.layout.item, null);
TextView textView=inflate.findViewById(R.id.tv);
textView.setText(list.get(i).getName()+""+ list.get(i).getAddress());
return inflate;
}
}