蓝牙原理Android代码实现
作者:李小白兔 时间:2023-07-10 20:25:31
本文实例为大家分享了Android实现蓝牙原理代码,供大家参考,具体内容如下
package com.example.se7en.testbluetooth;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class MainActivity extends Activity
{
private BluetoothAdapter adapter;
private MyReceiver receiver;
private ListView mListView;
private List<BluetoothDevice> mDevices;
private com.example.se7en.testbluetooth.DeviceAdapter mDeviceAdapter;
private BluetoothSocket mSocket;
private EditText et;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.lv);
et = (EditText) findViewById(R.id.et);
adapter = BluetoothAdapter.getDefaultAdapter();
BlueToothHandler.getInstance().setContext(this);
init();
}
private void init()
{
initListView();
// 判断是否支持蓝牙功能
if (adapter == null)
{
Toast.makeText(this, "该手机不支持蓝牙功能", Toast.LENGTH_LONG).show();
return;
}
// 判断蓝牙功能是否打开
if (!adapter.isEnabled())
{
// 强行打开
// adapter.enable();
Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(intent);
}
IntentFilter filter = new IntentFilter();
// 注册扫描开始的广播
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
// 注册扫描结束的广播
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
// 注册扫描已经找到设备的广播
filter.addAction(BluetoothDevice.ACTION_FOUND);
receiver = new MyReceiver();
registerReceiver(receiver, filter);
startBlueServer();
}
/**
* 启动蓝牙服务
*/
private void startBlueServer()
{
new Thread()
{
@Override
public void run()
{
while (true)
{
try
{
BluetoothServerSocket serverSocket = adapter
.listenUsingRfcommWithServiceRecord(
"蓝牙服务",
UUID.fromString("997f1b20-b4a0-45ea-a7dd-b2097299b1f6"));
mSocket = serverSocket.accept();//阻塞
BlueToothHandler.getInstance().setSocket(mSocket);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}.start();
}
public void doClick(View view)
{
switch (view.getId())
{
case R.id.btn:
mDevices.clear();
mDeviceAdapter.notifyDataSetChanged();
// 判断是否正在扫描,如果没在扫描那么就开始扫描
if (!adapter.isDiscovering() && adapter.startDiscovery())
{
Toast.makeText(this, "开始扫描周围的蓝牙设备", Toast.LENGTH_LONG)
.show();
}
break;
case R.id.send:
if ("".equals(et.getText().toString()))
{
Toast.makeText(this, "内容不能为空", Toast.LENGTH_SHORT).show();
return;
}
if (BlueToothHandler.getInstance().getSocket() == null)
{
Toast.makeText(this, "还未连接设备", Toast.LENGTH_SHORT).show();
return;
}
new Thread()
{
@Override
public void run()
{
BlueToothHandler.getInstance().sendMessage(
et.getText().toString());
}
}.start();
break;
case R.id.dis:
BlueToothHandler.getInstance().closeSocket();
break;
}
}
@Override
protected void onDestroy()
{
super.onDestroy();
if (receiver != null)
{
// 注销广播
unregisterReceiver(receiver);
}
}
private void initListView()
{
mDevices = new ArrayList<BluetoothDevice>();
mDeviceAdapter = new com.example.se7en.testbluetooth.DeviceAdapter(this, mDevices);
mListView.setAdapter(mDeviceAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
try
{
mSocket = mDevices
.get(position)
.createRfcommSocketToServiceRecord(
UUID.fromString("997f1b20-b4a0-45ea-a7dd-b2097299b1f6"));
mSocket.connect();//阻塞
Toast.makeText(MainActivity.this, "连接成功",
Toast.LENGTH_SHORT).show();
BlueToothHandler.getInstance().setSocket(mSocket);
}
catch (IOException e)
{
e.printStackTrace();
}
}
});
}
public class MyReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED))
{
Log.i("info", "开始扫描");
}
else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED))
{
Log.i("info", "扫描结束");
}
else if (action.equals(BluetoothDevice.ACTION_FOUND))
{
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.i("info", device.getName() + ":" + device.getAddress());
if (mDevices != null)
{
for (int i = 0; i < mDevices.size(); i++)
{
if (device.getAddress()
.equals(mDevices.get(i).getAddress()))
{
return;
}
}
mDevices.add(device);
mDeviceAdapter.notifyDataSetChanged();
}
}
}
}
}
标签:Android,蓝牙
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java虚拟机内存区域划分详解
2023-06-20 22:04:24
![](https://img.aspxhome.com/file/2023/6/57516_0s.png)
浅析java 10中的var关键字用法
2021-12-23 15:18:17
JAVA回顾:封装,继承,多态
2021-09-26 20:17:59
![](https://img.aspxhome.com/file/2023/7/67627_0s.png)
monkeyrunner之安卓开发环境搭建教程(1)
2023-02-24 06:24:22
![](https://img.aspxhome.com/file/2023/9/137999_0s.png)
c#哈希算法的实现方法及思路
2023-10-22 01:48:08
![](https://img.aspxhome.com/file/2023/3/113803_0s.png)
spring框架cacheAnnotation缓存注释声明解析
2022-04-14 17:13:05
![](https://img.aspxhome.com/file/2023/3/65323_0s.png)
java利用url实现网页内容的抓取
2023-05-14 11:10:21
![](https://img.aspxhome.com/file/2023/4/87214_0s.jpg)
Android下拉刷新官方版
2021-11-25 06:52:51
![](https://img.aspxhome.com/file/2023/6/139556_0s.gif)
Android中实现Webview顶部带进度条的方法
2022-04-22 15:45:43
![](https://img.aspxhome.com/file/2023/2/116142_0s.png)
Java中将File转化为MultipartFile的操作
2021-07-05 21:25:32
Java 中的变量类型
2022-05-27 20:00:19
SpringBoot带你实现一个点餐小程序
2022-04-01 23:38:08
![](https://img.aspxhome.com/file/2023/4/64914_0s.png)
c#重写TabControl控件实现关闭按钮的方法
2022-06-15 01:07:58
![](https://img.aspxhome.com/file/2023/6/108626_0s.png)
Unity动画混合树实例详解
2022-06-17 14:41:27
![](https://img.aspxhome.com/file/2023/8/104298_0s.gif)
springboot 自定义异常并捕获异常返给前端的实现代码
2022-07-23 03:09:52
![](https://img.aspxhome.com/file/2023/5/71625_0s.png)
C#导出Excel的示例详解
2021-12-03 01:55:51
Java类的初始化顺序知识点总结
2021-09-23 23:19:26
![](https://img.aspxhome.com/file/2023/8/63748_0s.png)
C#实现简单获取及设置Session类
2021-07-09 06:19:27
Springboot整合Freemarker的实现详细过程
2023-01-23 22:09:57
![](https://img.aspxhome.com/file/2023/4/84164_0s.jpg)
java的引用类型的详细介绍
2022-05-15 09:34:40