Android Bluetooth蓝牙操作

一、什么是蓝牙(Bluetooth)

1.Bluetooth是目前使用的最广泛的无线通讯协议;

2.主要针对短距离设备通讯(10M);

3.常用语连接耳机、鼠标和移动通讯设备等;

二、获取蓝牙状态、操作蓝牙

在AndroidMainfest.xml声明蓝牙权限

<uses-permission android:name=”android.permission.BLUETOOTH”/>

<uses-permission android:name=”android.permission.BLUETOOTH_ADMIN”/>

获取蓝牙状态,开启蓝牙:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
            if(adapter != null){
                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("没有蓝牙设备");
            }

扫描蓝牙,获得扫描对象:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.lpq.bluetooth;

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.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    private BluetoothReceiver bluetoothReceiver = null;
    private BluetoothAdapter bluetoothAdapter = null;
    private Button scanButton = null;
    private Button discoverButton = null;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        discoverButton = (Button)findViewById(R.id.discoverButtonId);
        discoverButton.setOnClickListener(new DiscoverButtonListener());
       
        IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        bluetoothReceiver = new BluetoothReceiver();
        registerReceiver(bluetoothReceiver, intentFilter);
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        scanButton = (Button)findViewById(R.id.scanButtonId);
        System.out.println(scanButton);
        scanButton.setOnClickListener(new ScanButtonListener());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    private class BluetoothReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            String action = intent.getAction();
            if(BluetoothDevice.ACTION_FOUND.equals(action)){
                //将远程蓝牙适配器的对象取出
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                System.out.println(device.getAddress());
            }
        }
    }
   
    //修改蓝牙设备可见性
    private class DiscoverButtonListener implements OnClickListener{
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            //可见状态持续时间
            discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 500);
            startActivity(discoverableIntent);
        }
    }
   
    //扫描蓝牙设备
    private class ScanButtonListener implements OnClickListener{
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            bluetoothAdapter.startDiscovery();
        }
    }
   
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        unregisterReceiver(bluetoothReceiver);
        super.onDestroy();
    }
}

除非注明,饮水思源博客文章均为原创,转载请以链接形式标明本文地址

本文地址:http://www.alonemonkey.com/android-bluetooth.html

本文链接:http://www.alonemonkey.com/android-bluetooth.html