<div class="content">
<p>
<span style="font-size: 16px;"><strong>相关类:</strong></span>
</p>
<p>
<br/>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> 1.BluetoothAdapter </span><span style="margin: 0px; padding: 0px;">顾名思义,蓝牙适配器,直到我们建立bluetoothSocket连接之前,都要不断操作它</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> BluetoothAdapter</span><span style="margin: 0px; padding: 0px;">里的方法很多,常用的有以下几个:</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> cancelDiscovery() 取消搜索</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> disable()</span><span style="margin: 0px; padding: 0px;">关闭蓝牙</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> enable()</span><span style="margin: 0px; padding: 0px;">打开蓝牙,这个方法打开蓝牙不会弹出提示,更多的时候我们需要问下用户是否打开,一下这两行代码同样是打开蓝牙,不过会提示用户:</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"></span></span>
</p>
<pre class="brush:java;toolbar:true">Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); //打开蓝牙设备startActivity(intent);</pre>
<p>
<br/>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> getDefaultAdapter()</span><span style="margin: 0px; padding: 0px;">获取默认BluetoothAdapter,实际上,也只有这一种方法获取BluetoothAdapter</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> getName()</span><span style="margin: 0px; padding: 0px;">获取本地蓝牙名称</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> getRemoteDevice(String address)</span><span style="margin: 0px; padding: 0px;">根据蓝牙地址获取远程蓝牙设备</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> getState()</span><span style="margin: 0px; padding: 0px;">获取本地蓝牙适配器当前状态(感觉可能调试的时候更需要)</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> isDiscovering()</span><span style="margin: 0px; padding: 0px;">判断当前是否正在查找设备,是返回true</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> isEnabled()</span><span style="margin: 0px; padding: 0px;">判断蓝牙是否打开,已打开返回true,否则,返回false</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> listenUsingRfcommWithServiceRecord(String name,UUID uuid)</span><span style="margin: 0px; padding: 0px;">根据名称,UUID创建并返回BluetoothServerSocket,这是创建BluetoothSocket服务器端的第一步</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> startDiscovery()</span><span style="margin: 0px; padding: 0px;">开始搜索,这是搜索的第一步</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> 2.BluetoothDevice</span><span style="margin: 0px; padding: 0px;">看名字就知道,这个类描述了一个蓝牙设备</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> createRfcommSocketToServiceRecord(UUIDuuid)</span><span style="margin: 0px; padding: 0px;">根据UUID创建并返回一个BluetoothSocket </span></span>这个方法也是我们获取BluetoothDevice的目的——创建BluetoothSocket
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px;"> 这个类其他的方法,如getAddress(),getName(),同BluetoothAdapter</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> 3.BluetoothServerSocket</span><span style="margin: 0px; padding: 0px;">如果去除了Bluetooth相信大家一定再熟悉不过了,既然是Socket,方法就应该都差不多,</span></span>这个类一种只有三个方法,阻塞的socket而且是
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px;"> 两个重载的accept(),accept(inttimeout)两者的区别在于后面的方法指定了过时时间,需要注意的是,执行这两个方法的时候,直到接收到了客户端的请求(或是过期之后),都会阻塞线程,应该放在新线程里运行!</span></span>还有一点需要注意的是,这两 个方法都返回一个BluetoothSocket,最后的连接也是服务器端与客户端的两个BluetoothSocket的连接
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px;"> close()</span><span style="margin: 0px; padding: 0px;">这个就不用说了吧,翻译一下——关闭!</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> 4.BluetoothSocket,</span><span style="margin: 0px; padding: 0px;">跟BluetoothServerSocket相对,是客户端</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> 一共5个方法,不出意外,都会用到</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> close(),</span><span style="margin: 0px; padding: 0px;">关闭</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> connect()</span><span style="margin: 0px; padding: 0px;">连接</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> getInptuStream()</span><span style="margin: 0px; padding: 0px;">获取输入流</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> getOutputStream()</span><span style="margin: 0px; padding: 0px;">获取输出流</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> getRemoteDevice()</span><span style="margin: 0px; padding: 0px;">获取远程设备,这里指的是获取bluetoothSocket指定连接的那个远程蓝牙设备</span></span>
</p><br/>
<p>
<br/>
</p>
<p>
权限:
</p>
<p>
<span style="white-space: pre;"></span><uses-permission android:name="android.permission.BLUETOOTH" />
</p>
<p>
<span style="white-space: pre;"></span><uses-permisson android:name="android.permission.BLUETOOTH_ADMIN" /> <!--操作蓝牙设备-->
</p>
<p>
广播
</p>
<p>
<span style="white-space: pre;"> </span>BluetoothDevice.ACTION_FOUND//查找到一个蓝牙设备时广播<br/> <span style="white-space: pre;"></span> BluetoothAdapter.ACTION_DISCOVERY_FINISHED//查找结束时 广播<br/> <span style="white-space: pre;"></span> BluetoothDevice.ACTION_BOND_STATE_CHANGED //当蓝牙设备 匹配或者取消匹配时广播
</p>
<p>
1.打开蓝牙<span style="white-space: pre;"> </span>
</p>
<p>
<span style="white-space: pre;"></span>
</p>
<pre class="brush:java;toolbar:true">BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if(adapter!=null){
if(!adapter.isEnabled()){ //查看蓝牙是否开启
Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); //打开蓝牙设备,这两句会出现提示框 enable()方法打开的蓝牙不会出现提示
startActivity(intent);
}
Set<BluetoothDevice> devices=adapter.getBondedDevices();//得到所有配对的蓝牙视频器
if(devices!=null&&devices.size()>0){
Iterator<BluetoothDevice> ite=devices.iterator();
for (BluetoothDevice bluetoothDevice : devices) {
Toast.makeText(MainActivity.this, bluetoothDevice.getAddress()+"", Toast.LENGTH_SHORT).show();
}
}
}</pre>2 打开蓝牙的可见性,可以被其他蓝牙检测到
<p>
<br/>
</p>
<p>
<br/>
</p>
<pre class="brush:java;toolbar:true">Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); //设置可见时间 最高300秒
startActivity(intent);</pre>3 扫描设备
<p>
<br/>
</p>
<p>
<br/>
</p>
<pre class="brush:java;toolbar:true">BluetoothAdapter.getDefaultAdapter().startDiscovery();//开始扫描 异步调用 查找到一个设备后发送一广播 //注册接收蓝牙广播的
IntentFilter filter=new IntentFilter(BluetoothDevice.ACTION_FOUND); //当接收到广播的
registerReceiver(new BluetoothReceiver(), filter); 下面是广播接收器
class BluetoothReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); Toast.makeText(MainActivity.this, device.getAddress()+"", Toast.LENGTH_SHORT).show(); } }</pre>
<p>
4 匹配设配(利用反射机制)
</p>
<p>
<br/>
</p>
<pre class="brush:plain;toolbar:true">Method createBondMethod = BluetoothDevice.class.getMethod("createBond"); Boolean a = (Boolean) createBondMethod.invoke(device1</pre>5连接(手头没俩手机.网上百度的..明天去搞个有错再修正,先记录下来)
<p>
<br/>
</p>
<p>
方法1:使用反射
</p>
<p>
<br/>
</p>
<pre class="brush:java;toolbar:true">Method m = null;
try {
m = device.getClass().getMethod("createRfcommSocket",
new Class[] { int.class });
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
try {
btSocket = (BluetoothSocket) m.invoke(device, 1);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
try {
btSocket.connect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}</pre>6 发送文件
<p>
<br/>
</p>
<p>
<br/>
</p>
<pre class="brush:java;toolbar:true">/*
* 发送一指定的文件到其它蓝牙设备
*/
ContentValues cv = new ContentValues();
cv.put("uri", FilePath);
cv.put("destination", address);
cv.put("direction", 0);
Long ts = System.currentTimeMillis();
cv.put("timestamp", ts);
getContentResolver().insert(Uri.parse("content://com.android.bluetooth.opp/btopp"), cv);
btSocket.close();</pre>
<pre class="brush:java;toolbar:true">/*
* 下面一段是通过intent的方式发送文件
* 与上面一段的不同在于,该方式会打开一个数据分享方式列表,如蓝牙,短信,Email 等
* 选择蓝牙方式后也是可以发送到其它蓝牙设备的
* 只不过偶尔也会抛出一个ioException异常,所以健壮性还有待加强,如添加try/catch模块
*/
// Intent intent = new Intent();
// intent.setAction(Intent.ACTION_SEND);
// intent.setType("image/jpg");
// intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File("/sdcard/test.jpg")) );
// startActivity(intent);</pre>
<br/>
<p>
<br/>
</p>
<p>
<br/> <br/>
</p>
<p>
<br/> <br/>
</p>
<pre class="brush:java;toolbar:true"><p></p></pre>
</div>
<p>
<span style="font-size: 16px;"><strong>相关类:</strong></span>
</p>
<p>
<br/>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> 1.BluetoothAdapter </span><span style="margin: 0px; padding: 0px;">顾名思义,蓝牙适配器,直到我们建立bluetoothSocket连接之前,都要不断操作它</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> BluetoothAdapter</span><span style="margin: 0px; padding: 0px;">里的方法很多,常用的有以下几个:</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> cancelDiscovery() 取消搜索</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> disable()</span><span style="margin: 0px; padding: 0px;">关闭蓝牙</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> enable()</span><span style="margin: 0px; padding: 0px;">打开蓝牙,这个方法打开蓝牙不会弹出提示,更多的时候我们需要问下用户是否打开,一下这两行代码同样是打开蓝牙,不过会提示用户:</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"></span></span>
</p>
<pre class="brush:java;toolbar:true">Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); //打开蓝牙设备startActivity(intent);</pre>
<p>
<br/>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> getDefaultAdapter()</span><span style="margin: 0px; padding: 0px;">获取默认BluetoothAdapter,实际上,也只有这一种方法获取BluetoothAdapter</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> getName()</span><span style="margin: 0px; padding: 0px;">获取本地蓝牙名称</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> getRemoteDevice(String address)</span><span style="margin: 0px; padding: 0px;">根据蓝牙地址获取远程蓝牙设备</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> getState()</span><span style="margin: 0px; padding: 0px;">获取本地蓝牙适配器当前状态(感觉可能调试的时候更需要)</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> isDiscovering()</span><span style="margin: 0px; padding: 0px;">判断当前是否正在查找设备,是返回true</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> isEnabled()</span><span style="margin: 0px; padding: 0px;">判断蓝牙是否打开,已打开返回true,否则,返回false</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> listenUsingRfcommWithServiceRecord(String name,UUID uuid)</span><span style="margin: 0px; padding: 0px;">根据名称,UUID创建并返回BluetoothServerSocket,这是创建BluetoothSocket服务器端的第一步</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> startDiscovery()</span><span style="margin: 0px; padding: 0px;">开始搜索,这是搜索的第一步</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> 2.BluetoothDevice</span><span style="margin: 0px; padding: 0px;">看名字就知道,这个类描述了一个蓝牙设备</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> createRfcommSocketToServiceRecord(UUIDuuid)</span><span style="margin: 0px; padding: 0px;">根据UUID创建并返回一个BluetoothSocket </span></span>这个方法也是我们获取BluetoothDevice的目的——创建BluetoothSocket
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px;"> 这个类其他的方法,如getAddress(),getName(),同BluetoothAdapter</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> 3.BluetoothServerSocket</span><span style="margin: 0px; padding: 0px;">如果去除了Bluetooth相信大家一定再熟悉不过了,既然是Socket,方法就应该都差不多,</span></span>这个类一种只有三个方法,阻塞的socket而且是
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px;"> 两个重载的accept(),accept(inttimeout)两者的区别在于后面的方法指定了过时时间,需要注意的是,执行这两个方法的时候,直到接收到了客户端的请求(或是过期之后),都会阻塞线程,应该放在新线程里运行!</span></span>还有一点需要注意的是,这两 个方法都返回一个BluetoothSocket,最后的连接也是服务器端与客户端的两个BluetoothSocket的连接
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px;"> close()</span><span style="margin: 0px; padding: 0px;">这个就不用说了吧,翻译一下——关闭!</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> 4.BluetoothSocket,</span><span style="margin: 0px; padding: 0px;">跟BluetoothServerSocket相对,是客户端</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> 一共5个方法,不出意外,都会用到</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> close(),</span><span style="margin: 0px; padding: 0px;">关闭</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> connect()</span><span style="margin: 0px; padding: 0px;">连接</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> getInptuStream()</span><span style="margin: 0px; padding: 0px;">获取输入流</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> getOutputStream()</span><span style="margin: 0px; padding: 0px;">获取输出流</span></span>
</p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">
<span style="margin: 0px; padding: 0px; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px;"> getRemoteDevice()</span><span style="margin: 0px; padding: 0px;">获取远程设备,这里指的是获取bluetoothSocket指定连接的那个远程蓝牙设备</span></span>
</p><br/>
<p>
<br/>
</p>
<p>
权限:
</p>
<p>
<span style="white-space: pre;"></span><uses-permission android:name="android.permission.BLUETOOTH" />
</p>
<p>
<span style="white-space: pre;"></span><uses-permisson android:name="android.permission.BLUETOOTH_ADMIN" /> <!--操作蓝牙设备-->
</p>
<p>
广播
</p>
<p>
<span style="white-space: pre;"> </span>BluetoothDevice.ACTION_FOUND//查找到一个蓝牙设备时广播<br/> <span style="white-space: pre;"></span> BluetoothAdapter.ACTION_DISCOVERY_FINISHED//查找结束时 广播<br/> <span style="white-space: pre;"></span> BluetoothDevice.ACTION_BOND_STATE_CHANGED //当蓝牙设备 匹配或者取消匹配时广播
</p>
<p>
1.打开蓝牙<span style="white-space: pre;"> </span>
</p>
<p>
<span style="white-space: pre;"></span>
</p>
<pre class="brush:java;toolbar:true">BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if(adapter!=null){
if(!adapter.isEnabled()){ //查看蓝牙是否开启
Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); //打开蓝牙设备,这两句会出现提示框 enable()方法打开的蓝牙不会出现提示
startActivity(intent);
}
Set<BluetoothDevice> devices=adapter.getBondedDevices();//得到所有配对的蓝牙视频器
if(devices!=null&&devices.size()>0){
Iterator<BluetoothDevice> ite=devices.iterator();
for (BluetoothDevice bluetoothDevice : devices) {
Toast.makeText(MainActivity.this, bluetoothDevice.getAddress()+"", Toast.LENGTH_SHORT).show();
}
}
}</pre>2 打开蓝牙的可见性,可以被其他蓝牙检测到
<p>
<br/>
</p>
<p>
<br/>
</p>
<pre class="brush:java;toolbar:true">Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); //设置可见时间 最高300秒
startActivity(intent);</pre>3 扫描设备
<p>
<br/>
</p>
<p>
<br/>
</p>
<pre class="brush:java;toolbar:true">BluetoothAdapter.getDefaultAdapter().startDiscovery();//开始扫描 异步调用 查找到一个设备后发送一广播 //注册接收蓝牙广播的
IntentFilter filter=new IntentFilter(BluetoothDevice.ACTION_FOUND); //当接收到广播的
registerReceiver(new BluetoothReceiver(), filter); 下面是广播接收器
class BluetoothReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); Toast.makeText(MainActivity.this, device.getAddress()+"", Toast.LENGTH_SHORT).show(); } }</pre>
<p>
4 匹配设配(利用反射机制)
</p>
<p>
<br/>
</p>
<pre class="brush:plain;toolbar:true">Method createBondMethod = BluetoothDevice.class.getMethod("createBond"); Boolean a = (Boolean) createBondMethod.invoke(device1</pre>5连接(手头没俩手机.网上百度的..明天去搞个有错再修正,先记录下来)
<p>
<br/>
</p>
<p>
方法1:使用反射
</p>
<p>
<br/>
</p>
<pre class="brush:java;toolbar:true">Method m = null;
try {
m = device.getClass().getMethod("createRfcommSocket",
new Class[] { int.class });
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
try {
btSocket = (BluetoothSocket) m.invoke(device, 1);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
try {
btSocket.connect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}</pre>6 发送文件
<p>
<br/>
</p>
<p>
<br/>
</p>
<pre class="brush:java;toolbar:true">/*
* 发送一指定的文件到其它蓝牙设备
*/
ContentValues cv = new ContentValues();
cv.put("uri", FilePath);
cv.put("destination", address);
cv.put("direction", 0);
Long ts = System.currentTimeMillis();
cv.put("timestamp", ts);
getContentResolver().insert(Uri.parse("content://com.android.bluetooth.opp/btopp"), cv);
btSocket.close();</pre>
<pre class="brush:java;toolbar:true">/*
* 下面一段是通过intent的方式发送文件
* 与上面一段的不同在于,该方式会打开一个数据分享方式列表,如蓝牙,短信,Email 等
* 选择蓝牙方式后也是可以发送到其它蓝牙设备的
* 只不过偶尔也会抛出一个ioException异常,所以健壮性还有待加强,如添加try/catch模块
*/
// Intent intent = new Intent();
// intent.setAction(Intent.ACTION_SEND);
// intent.setType("image/jpg");
// intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File("/sdcard/test.jpg")) );
// startActivity(intent);</pre>
<br/>
<p>
<br/>
</p>
<p>
<br/> <br/>
</p>
<p>
<br/> <br/>
</p>
<pre class="brush:java;toolbar:true"><p></p></pre>
</div>