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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_bluetooth);// 得到蓝牙adapterBluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();if (bluetoothAdapter == null) {// 设备不支持蓝牙}// 打开蓝牙 - 用户是否打开蓝牙,可在onActivityResult()中判断if (!bluetoothAdapter.isEnabled()) {Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);}// 查找已配对过的设备Set pairedDevices = bluetoothAdapter.getBondedDevices();if (pairedDevices.size() > 0) {for (BluetoothDevice device : pairedDevices) {// 得到设备对象device}}// 发现周围的蓝牙设备 - 异步扫描周围的蓝牙设备,使用监听广播的方式,得到扫描到的设备// 请见对象deviceReceiverbluetoothAdapter.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,注意同一台设备可能会有多次返回}}};@Overrideprotected 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) {// 用户同意设备被扫描到}}}@Overrideprotected 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 localHashMap = null;try {localIntent = new Intent();localIntent.setAction(Intent.ACTION_SEND); // 设置为分享ACTIONlocalIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); // 添加传输文件// localIntent.putExtra(Intent.EXTRA_STREAM,// Uri.fromFile(new File(localApplicationInfo.sourceDir)));localIntent.setType("*/*"); // 设置应用的打开方式List localList = localPackageManager.queryIntentActivities(localIntent, 0); // 查找所有可以处理该intent的localHashMap = new HashMap();Iterator 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 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); // 请求处理intentreturn;}}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部