蓝牙
程序员文章站
2022-04-19 09:35:40
蓝牙一、设置权限
蓝牙
一、设置权限
<uses-permission android:name="android.permission.BLUETOOTH"></uses-permission>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
二、布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/open_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="打开蓝牙" />
<Button
android:id="@+id/close_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="关闭蓝牙" />
<Button
android:id="@+id/getName_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="获取本机蓝牙名称" />
<Button
android:id="@+id/nearby_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="扫描附近蓝牙" />
<Button
android:id="@+id/show_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="展示已配对设备" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/mynearbyReceiver_id"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/myshowReceiver_id"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
三、判断权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{
Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
}, 100);
}
四、打开,关闭蓝牙
//打开蓝牙
openId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
//启动蓝牙
intent.setAction(BluetoothAdapter.ACTION_REQUEST_ENABLE);
intent.setAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivity(intent);
}
});
//关闭蓝牙
closeId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获得本机蓝牙适配器
BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
//关闭本机蓝牙设备
defaultAdapter.disable();
}
});
五、获取本机蓝牙名字
//获取本机名字
getNameId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
String name = defaultAdapter.getName();
String address = defaultAdapter.getAddress();
Toast.makeText(MainActivity.this, name + ":" + address, Toast.LENGTH_SHORT).show();
}
});
六、扫描附近蓝牙
//注册内容提供者
myReceiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
registerReceiver(myReceiver, intentFilter);
nearbyId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
//开始扫描
defaultAdapter.startDiscovery();
}
});
内容提供者
class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(BluetoothDevice.ACTION_FOUND)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.i(TAG, "onReceive: " + device.getName() + ":" + device.getAddress());
nearbyId_list.add(device);
nearbyId_adapter.notifyDataSetChanged();
}
}
}
七、显示配对蓝牙
//显示已配对的蓝牙设备
showId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> bondedDevices = defaultAdapter.getBondedDevices();
for (BluetoothDevice bondedDevice : bondedDevices) {
Log.i(TAG, "onClick: " + "配对" + bondedDevice.getName() + ":" + bondedDevice.getAddress());
}
showId_list.addAll(bondedDevices);
showId_adapter.notifyDataSetChanged();
}
});
监听接口
public interface MyListener {
void onClick(int position, View view);
}
适配器
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private Context context;
private List<BluetoothDevice> list;
private MyListener myListener;
public void setMyListener(MyListener myListener) {
this.myListener = myListener;
}
public MyAdapter(Context context, List<BluetoothDevice> list) {
this.context = context;
this.list = list;
}
//连接布局
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(context).inflate(R.layout.layout_item, null);
return new MyViewHolder(inflate);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
holder.textView_item.setText(list.get(position).getName() + ":" + list.get(position).getAddress());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myListener.onClick(position, v);
}
});
}
@Override
public int getItemCount() {
return list.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView textView_item;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
textView_item = itemView.findViewById(R.id.text_item);
}
}
}
Activity代码
//附近蓝牙设备展示
nearbyId_adapter = new MyAdapter(this, nearbyId_list);
mynearbyReceiverId.setAdapter(nearbyId_adapter);
mynearbyReceiverId.setLayoutManager(new LinearLayoutManager(this));
mynearbyReceiverId.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
nearbyId_adapter.setMyListener(new MyListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(int position, View view) {
Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
//发起连接请求
nearbyId_list.get(position).createBond();
}
});
//展示已配对设备
showId_adapter = new MyAdapter(this, showId_list);
myshowReceiverId.setAdapter(showId_adapter);
myshowReceiverId.setLayoutManager(new LinearLayoutManager(this));
myshowReceiverId.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
showId_adapter.setMyListener(new MyListener() {
@Override
public void onClick(int position, View view) {
Toast.makeText(MainActivity.this, "准备建立发送数据", Toast.LENGTH_SHORT).show();
}
});
}
Activity整体代码
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private Button openId;
private Button closeId;
private Button getNameId;
private Button nearbyId;
private Button showId;
private RecyclerView mynearbyReceiverId;
private RecyclerView myshowReceiverId;
private MyReceiver myReceiver;
private List<BluetoothDevice> nearbyId_list = new ArrayList<>();
private List<BluetoothDevice> showId_list = new ArrayList<>();
private MyAdapter nearbyId_adapter;
private MyAdapter showId_adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openId = (Button) findViewById(R.id.open_id);
closeId = (Button) findViewById(R.id.close_id);
getNameId = (Button) findViewById(R.id.getName_id);
nearbyId = (Button) findViewById(R.id.nearby_id);
showId = (Button) findViewById(R.id.show_id);
mynearbyReceiverId = (RecyclerView) findViewById(R.id.mynearbyReceiver_id);
myshowReceiverId = (RecyclerView) findViewById(R.id.myshowReceiver_id);
myReceiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
registerReceiver(myReceiver, intentFilter);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{
Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
}, 100);
}
//打开蓝牙
openId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
//启动蓝牙
intent.setAction(BluetoothAdapter.ACTION_REQUEST_ENABLE);
intent.setAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivity(intent);
}
});
//关闭蓝牙
closeId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获得本机蓝牙适配器
BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
//关闭本机蓝牙设备
defaultAdapter.disable();
}
});
//获取本机名字
getNameId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
String name = defaultAdapter.getName();
String address = defaultAdapter.getAddress();
Toast.makeText(MainActivity.this, name + ":" + address, Toast.LENGTH_SHORT).show();
}
});
//扫描附近蓝牙
nearbyId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
//开始扫描
defaultAdapter.startDiscovery();
}
});
//显示已配对的蓝牙设备
showId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> bondedDevices = defaultAdapter.getBondedDevices();
for (BluetoothDevice bondedDevice : bondedDevices) {
Log.i(TAG, "onClick: " + "配对" + bondedDevice.getName() + ":" + bondedDevice.getAddress());
}
showId_list.addAll(bondedDevices);
showId_adapter.notifyDataSetChanged();
}
});
//附近蓝牙设备展示
nearbyId_adapter = new MyAdapter(this, nearbyId_list);
mynearbyReceiverId.setAdapter(nearbyId_adapter);
mynearbyReceiverId.setLayoutManager(new LinearLayoutManager(this));
mynearbyReceiverId.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
nearbyId_adapter.setMyListener(new MyListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(int position, View view) {
Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
//发起连接请求
nearbyId_list.get(position).createBond();
}
});
//展示已配对设备
showId_adapter = new MyAdapter(this, showId_list);
myshowReceiverId.setAdapter(showId_adapter);
myshowReceiverId.setLayoutManager(new LinearLayoutManager(this));
myshowReceiverId.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
showId_adapter.setMyListener(new MyListener() {
@Override
public void onClick(int position, View view) {
Toast.makeText(MainActivity.this, "准备建立发送数据", Toast.LENGTH_SHORT).show();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(myReceiver);
}
class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(BluetoothDevice.ACTION_FOUND)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.i(TAG, "onReceive: " + device.getName() + ":" + device.getAddress());
nearbyId_list.add(device);
nearbyId_adapter.notifyDataSetChanged();
}
}
}
}
本文地址:https://blog.csdn.net/Sun_Sees_You/article/details/107475536
上一篇: Android基础知识—数据持久化
下一篇: Photoshop制作逼真生动的下雨情景
推荐阅读
-
小米电视4A怎么连接蓝牙音箱?
-
漫步者LolliPods Pro蓝牙耳机怎么样?漫步者LolliPods Pro降噪蓝牙耳机测评
-
为什么蓝牙在 Windows 11 中不起作用?Win11中蓝牙不起作用的解决方法
-
Windows 10新版18985推送:改善蓝牙快速配对体验
-
ios蓝牙开发学习笔记(四)ios蓝牙应用的后台处理
-
小米蓝牙音箱怎么样?小米蓝牙音箱开箱评测
-
iOS蓝牙打印价签:【商品条码、品名、零售价、规格信息等】(商品名称支持换行显示)
-
小程序蓝牙开发会不会取代Android端或者iOS端蓝牙开发?
-
微信小程序蓝牙连接小票打印机实例代码详解
-
Android 取消蓝牙配对框实现自动配对功能