android开发笔记蓝牙

<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;">&nbsp; &nbsp; &nbsp; &nbsp;1.BluetoothAdapter&nbsp;</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;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;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;">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;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;">&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;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;">&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;enable()</span><span style="margin: 0px; padding: 0px;">打开蓝牙,这个方法打开蓝牙不会弹出提示,更多的时候我们需要问下用户是否打开,一下这两行代码同样是打开蓝牙,不过会提示用户:</span></span>&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;
    </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;">&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;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;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;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;">&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;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;">&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;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;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;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;">&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;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;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;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;">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;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;">&nbsp; &nbsp; 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;">&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;createRfcommSocketToServiceRecord(UUIDuuid)</span><span style="margin: 0px; padding: 0px;">根据UUID创建并返回一个BluetoothSocket &nbsp;&nbsp;</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;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 这个类其他的方法,如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;">&nbsp; &nbsp; 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;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;两个重载的accept(),accept(inttimeout)两者的区别在于后面的方法指定了过时时间,需要注意的是,执行这两个方法的时候,直到接收到了客户端的请求(或是过期之后),都会阻塞线程,应该放在新线程里运行!</span></span>还有一点需要注意的是,这两 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 个方法都返回一个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;">&nbsp; &nbsp; &nbsp; &nbsp; 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;">&nbsp; &nbsp; 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;">&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;一共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;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;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;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;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;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;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;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;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;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;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>&lt;uses-permission android:name=&quot;android.permission.BLUETOOTH&quot; /&gt;
    </p>
    <p>
        <span style="white-space: pre;"></span>&lt;uses-permisson android:name=&quot;android.permission.BLUETOOTH_ADMIN&quot; /&gt; &lt;!--操作蓝牙设备--&gt;
    </p>
    <p>
        广播
    </p>
    <p>
        &nbsp; &nbsp;<span style="white-space: pre;"> </span>BluetoothDevice.ACTION_FOUND//查找到一个蓝牙设备时广播<br/> <span style="white-space: pre;"></span>&nbsp; &nbsp; BluetoothAdapter.ACTION_DISCOVERY_FINISHED//查找结束时 广播<br/> <span style="white-space: pre;"></span>&nbsp; &nbsp; 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&lt;BluetoothDevice&gt; devices=adapter.getBondedDevices();//得到所有配对的蓝牙视频器
if(devices!=null&amp;&amp;devices.size()&gt;0){
Iterator&lt;BluetoothDevice&gt; ite=devices.iterator();
for (BluetoothDevice bluetoothDevice : devices) {
Toast.makeText(MainActivity.this, bluetoothDevice.getAddress()+&quot;&quot;, Toast.LENGTH_SHORT).show();
}
}

}</pre>2&nbsp;打开蓝牙的可见性,可以被其他蓝牙检测到
    <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()+&quot;&quot;, Toast.LENGTH_SHORT).show(); }  }</pre> 
 
    <p>
        4 匹配设配(利用反射机制)
    </p>
    <p>
        <br/>
    </p>
    <pre class="brush:plain;toolbar:true">Method createBondMethod = BluetoothDevice.class.getMethod(&quot;createBond&quot;); 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(&quot;createRfcommSocket&quot;,
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(&quot;uri&quot;, FilePath);
cv.put(&quot;destination&quot;, address);
cv.put(&quot;direction&quot;, 0);
Long ts = System.currentTimeMillis();
cv.put(&quot;timestamp&quot;, ts);
getContentResolver().insert(Uri.parse(&quot;content://com.android.bluetooth.opp/btopp&quot;), 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(&quot;image/jpg&quot;);
// intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(&quot;/sdcard/test.jpg&quot;)) );
// 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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值