android蓝牙操作
Posted 杨迈1949
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android蓝牙操作相关的知识,希望对你有一定的参考价值。
蓝牙基本操作
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 java.util.Set;
public class BluetoothActivity extends Activity
private static final int REQUEST_ENABLE_BT = 0x11;
private static final int REQUEST_ENABLE_DISCOVERY = 0x12;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth);
// 得到蓝牙adapter
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null)
// 设备不支持蓝牙
// 打开蓝牙 - 用户是否打开蓝牙,可在onActivityResult()中判断
if (!bluetoothAdapter.isEnabled())
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
// 查找已配对过的设备
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0)
for (BluetoothDevice device : pairedDevices)
// 得到设备对象device
// 发现周围的蓝牙设备 - 异步扫描周围的蓝牙设备,使用监听广播的方式,得到扫描到的设备
// 请见对象deviceReceiver
bluetoothAdapter.startDiscovery();
// 注册广播
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(deviceReceiver, filter);
// 关闭发现周围的蓝牙设备
bluetoothAdapter.cancelDiscovery();
// 当前蓝牙设备能被发现,在300秒内,可在onActivityResult()中判断用户是否同意,该方法会自动打开蓝牙
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivityForResult(discoverableIntent, REQUEST_ENABLE_DISCOVERY);
// 得到蓝牙mac地址
bluetoothAdapter.getAddress();
// 通过蓝牙mac地址,得到蓝牙设备
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
private final BroadcastReceiver deviceReceiver = new BroadcastReceiver()
public void onReceive(Context context, Intent intent)
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action))
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// 拿到发现得蓝牙设备对象device,注意同一台设备可能会有多次返回
;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if (resultCode == RESULT_CANCELED)
if (requestCode == REQUEST_ENABLE_BT)
// 用户没有打开蓝牙
else if (requestCode == REQUEST_ENABLE_DISCOVERY)
// 用户不想让设备被扫描到
else
if ((requestCode == REQUEST_ENABLE_BT))
// 用户打开了蓝牙
else if (requestCode == REQUEST_ENABLE_DISCOVERY)
// 用户同意设备被扫描到
@Override
protected void onDestroy()
// 注销广播
unregisterReceiver(deviceReceiver);
super.onDestroy();
蓝牙传输文件
使用默认的系统蓝牙应用,发送文件给另一台支持蓝牙设备
在Android系统中,使用蓝牙可以分享文件给另一台蓝牙设备,这样一种传输方式,使用的就是OBEX协议。按我的理解,所有蓝牙设备都支持OBEX协议。
在文件管理器中,可以分享文件,但是会弹出很多的分享目标:QQ,微信,蓝牙等…,若想实现这个功能,就需要直接调用蓝牙分享,下面代码使用的就是这个思路,摘录自:http://www.2cto.com/kf/201401/270305.html;
private void sendFile(Activity activity, File file)
PackageManager localPackageManager = activity.getPackageManager();
Intent localIntent = null;
HashMap<String, ActivityInfo> localHashMap = null;
try
localIntent = new Intent();
localIntent.setAction(Intent.ACTION_SEND); // 设置为分享ACTION
localIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); // 添加传输文件
// localIntent.putExtra(Intent.EXTRA_STREAM,
// Uri.fromFile(new File(localApplicationInfo.sourceDir)));
localIntent.setType("*/*"); // 设置应用的打开方式
List<ResolveInfo> localList = localPackageManager.queryIntentActivities(
localIntent, 0); // 查找所有可以处理该intent的
localHashMap = new HashMap<String, ActivityInfo>();
Iterator<ResolveInfo> localIterator1 = localList.iterator();
while (localIterator1.hasNext())
ResolveInfo resolveInfo = (ResolveInfo) localIterator1.next();
ActivityInfo localActivityInfo2 = resolveInfo.activityInfo;
String str = localActivityInfo2.applicationInfo.processName;
if (str.contains("bluetooth"))
localHashMap.put(str, localActivityInfo2); // 添加所有包含bluetooth关键字的程序
catch (Exception e)
Toast.makeText(BluetoothShareActivity.this, "蓝牙分享异常:" + e.getMessage(), Toast.LENGTH_SHORT).show();
if (localHashMap.size() == 0)
Toast.makeText(BluetoothShareActivity.this, "无蓝牙设备分享", Toast.LENGTH_SHORT).show();
ActivityInfo localActivityInfo1 = (ActivityInfo) localHashMap
.get("com.android.bluetooth");
if (localActivityInfo1 == null)
localActivityInfo1 = (ActivityInfo) localHashMap
.get("com.mediatek.bluetooth");
if (localActivityInfo1 == null)
Iterator<ActivityInfo> localIterator2 = localHashMap.values().iterator();
if (localIterator2.hasNext())
localActivityInfo1 = (ActivityInfo) localIterator2.next();
if (localActivityInfo1 != null)
localIntent.setComponent(new ComponentName(
localActivityInfo1.packageName, localActivityInfo1.name));
activity.startActivityForResult(localIntent, 4098); // 请求处理intent
return;
以上是关于android蓝牙操作的主要内容,如果未能解决你的问题,请参考以下文章