Android 蓝牙搜索

 

     最近学习了蓝牙的搜索,不过在调试的时候发现模拟器上要报错,后来想了一下,模拟器上没有蓝牙的模块,根本不能运行啊。所以像蓝牙这种需要硬件支持的项目,还是需要真机调试的。

 

    其他的不多说了吧,下面就直接上代码吧:

 

  下面呢就是蓝牙搜索的全部代码,里面包含的有蓝牙的打开,关闭以及搜索附近的蓝牙:

 

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package cn.chenwei.android.app;
 
import java.util.ArrayList;
 
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.MenuItem;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
 
public class SearchBluetoothActivity extends Activity {
    /** Called when the activity is first created. */
 
    BluetoothAdapter bluetoothAdapter = null;
    private ListView listview;
    ArrayAdapter<String> adapter = null;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        // 得到listview
        listview = (ListView) findViewById(R.id.list);
 
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();// 获取蓝牙适配器
 
        boolean flag = bluetoothAdapter.isEnabled();
        if (flag) {
            Toast.makeText(this, "蓝牙已经打开", Toast.LENGTH_SHORT).show();
        } else {
            bluetoothAdapter.enable();// 打开蓝牙设备
            Toast.makeText(this, "正在打开蓝牙", Toast.LENGTH_SHORT).show();
        }
 
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
 
        menu.add("搜索附近蓝牙设备");
 
        return super.onCreateOptionsMenu(menu);
    }
 
    boolean flag = true;// 线程停止的状态
 
    String[] strs = null;
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "正在搜索蓝牙", Toast.LENGTH_SHORT).show();
 
        // 动态注册
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
 
        registerReceiver(new MyBroadcastReceiver(), filter);
 
        new Thread(new Runnable() {
 
            public void run() {
                // TODO Auto-generated method stub
 
                while (true) {
                    bluetoothAdapter.startDiscovery();// 开始搜索
                    if (!flag) {
                        for (BluetoothDevice bd : list) {
                            System.out.println("搜索蓝牙" + bd.getName());
  
                        }
  
                    }
 
                    break;
                }
 
            }
        }).start();
 
        return super.onOptionsItemSelected(item);
    }
 
    // 创建数组用于储存 搜索到的蓝牙
    ArrayList<BluetoothDevice> list = null;
 
    // 蓝牙的三个状态,打开,发现,结束
    class MyBroadcastReceiver extends BroadcastReceiver {
 
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
 
            System.out.println("kaishile ");
            String action = intent.getAction();
            if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {// 开始
                Toast.makeText(SearchBluetoothActivity.this, "开始状态",
                        Toast.LENGTH_LONG).show();
                list = new ArrayList<BluetoothDevice>();
            } else if (BluetoothDevice.ACTION_FOUND.equals(action)) {// 搜索
 
                Toast.makeText(SearchBluetoothActivity.this, "发现状态",
                        Toast.LENGTH_LONG).show();
                BluetoothDevice bluetoothDevice = (BluetoothDevice) intent
                        .getExtras().get(BluetoothDevice.EXTRA_DEVICE);
 
                list.add(bluetoothDevice);
 
            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
                    .equals(action)) {//结束
                Toast.makeText(SearchBluetoothActivity.this, "结束状态",
                        Toast.LENGTH_LONG).show();
 
                flag = false;// 修改状态
                unregisterReceiver(this);// 注销广播
            }
        }
 
    }
 
}

 

      不过到这个地方,搜索蓝牙的功能还不能实现,因为缺少了调用蓝牙的权限:

1
2
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

 

 

   以上就是蓝牙搜索的全部代码,希望对朋友们有所帮助,各位朋友有什么问题请多多指教啊

    

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。