蓝牙Demo2
1.首先看布局,很简单,就有两个按钮,用于设置监听事件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/discoverbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置蓝牙的可见性" />
<Button
android:id="@+id/scannbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始扫描蓝牙设备" />
</LinearLayout>
2.接着看看实现的活动类
package com.wang;
import java.util.Iterator;
import java.util.Set;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class BluetoothActivity extends Activity {
private BluetoothAdapter bluetoothAdapter;
private Button scannbutton;
private Button discoverbutton;
private BluetoothReceiver bluetoothReceiver = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 创建一个对象IntentFilter 过滤是否发线蓝牙设备
IntentFilter intentFilter = new IntentFilter(
BluetoothDevice.ACTION_FOUND);
bluetoothReceiver = new BluetoothReceiver();
// 注册一个广播接收器,
registerReceiver(bluetoothReceiver, intentFilter);
// 得到默认是本地蓝牙适配器
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
discoverbutton = (Button) findViewById(R.id.discoverbutton);
scannbutton = (Button) findViewById(R.id.scannbutton);
scannbutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// d得到BluetoothAdapter的对象
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
// 判断是否为空
if (adapter != null) {
System.out.println("本机拥有蓝牙设备");
// 是否可用
if (!adapter.isEnabled()) {
// 得到允许用户打开蓝牙的意图
Intent intent = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(intent);
}
// 得到所有已经适配好的蓝牙设备
Set<BluetoothDevice> devices = adapter.getBondedDevices();
if (devices.size() > 0) {
for (Iterator iterator = devices.iterator(); iterator
.hasNext();) {
BluetoothDevice bluetoothDevice = (BluetoothDevice) iterator
.next();
// 输出远程蓝牙设备的地址
System.out.println(bluetoothDevice.getAddress());
}
}
} else {
System.out.println("没有蓝牙设备!!!");
}
bluetoothAdapter.startDiscovery();
}
});
// 设置蓝牙设备在500s可见的监听事件
discoverbutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 创建一个意图与一个给定的行动。
Intent discoverIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
// 蓝牙设备在500s的请求时间内可见
discoverIntent.putExtra(
BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 500);
startActivity(discoverIntent);
}
});
}
// ji接收广播事件
private class BluetoothReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 得到当前的动作
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// 如果1~10m内存在蓝牙设备的到他的地址
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
System.out.println(device.getAddress());
}
}
}
protected void onDestory() {
unregisterReceiver(bluetoothReceiver);
super.onDestroy();
}
}
3.亲、不要忘了添加权限哦!不然找了半天的错你也很难找到的
<!-- 蓝牙操作权限 -->
<uses-permission
android:name="android.permission.BLUETOOTH_ADMIN"
/>
<!--蓝牙使用权限 -->
<uses-permission
android:name="android.permission.BLUETOOTH"
/>
4.运行结果:由于模拟器上没有蓝牙设备,只能在自己手机上运行 ,所以呢,效果图没发传上。


至此,本篇已结束,如有不对的地方,欢迎您的建议与指正。同时期待您的关注,感谢您的阅读,谢谢!

如有侵权,请联系小编,小编对此深感抱歉,届时小编会删除文章,立即停止侵权行为,请您多多包涵。

既然都看到这里,领两个红包在走吧!
以下两个红包每天都可以领取
1.支付宝搜索 522398497,或扫码支付宝红包海报。

支付宝扫一扫,每天领取大红包
2.微信红包,微信扫一扫即可领取红包

微信扫一扫,每天领取微信红包
小礼物走一走,来简书关注我
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!