• Android蓝牙测试—发送一文件到另一蓝牙设备


    该测试程序是根据网上代码更改的,用于向另一蓝牙设备发送一图片文件。本文截图测试的是向PC上发送一指定图片(如果与要连接的设备未配对,会提示配对的)。

    需要注意以下几个方面:
    1. 传统的UUID方法(也是网络上流行的)连接其它蓝牙设备的方式根本行不通,在网络上搜索了很久终于找到一个替代的方法是可以工作的(详细见代码)
    2. 关于蓝牙设置的两个属性:“开启关闭”与“设置可见”,这是两个独立设置选项,但M9手机将它们设置成关联了,即打开了蓝牙设备就自动设置为可见了,而设 置为可见后蓝牙设备也就打开了(手机UI设置里面无法单独操作“设置可见”,但代码可以),所以这里也纠结了一段时间,相关代码部分有说明

    先看程序截图:
    1. M9手机截图:开启蓝牙并搜索设备后


    2. PC端截图:手机端点击搜索到的设备"wzc-0"后,PC端会提示权限操作


    3. PC端截图:当PC端允许操作后,接收从M9传来的图片(ubuntu系统蓝牙接收到文件会默认存储到:/home/yourname/下载)


    下面看代码:
    首先看AndroidManifest.xml文件:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.testBlueTooth"
    android:versionCode="1"
    android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".testBlueTooth" android:label="@string/app_name"
    android:launchMode="singleTask" android:screenOrientation="portrait">
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>
    </application>
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    </manifest>  

    需要注意两点:
    1. 最后两行的权限申明
    2. <activity>里面的强制竖屏属性设置:android:launchMode="singleTask" android:screenOrientation="portrait",避免纵横屏转换时程序异常退出!

    然后看布局文件main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content">
    <TextView android:id="@+id/TextView01" android:layout_height="wrap_content"
    android:text="手机蓝牙开关" android:layout_width="100dip"></TextView>
    <ToggleButton android:layout_height="wrap_content" android:text="蓝牙开关"
    android:layout_width="wrap_content" android:id="@+id/tbtnSwitch"></ToggleButton>
    <Button android:layout_height="wrap_content" android:text="本机蓝牙可见"
    android:id="@+id/btnDis" android:layout_width="160dip"></Button>
    </LinearLayout>
    <LinearLayout android:id="@+id/LinearLayout02" android:layout_width="wrap_content" android:layout_height="wrap_content">
    <Button android:layout_height="wrap_content" android:id="@+id/btnSearch"
    android:text="搜索设备" android:layout_width="160dip"></Button>
    <Button android:layout_height="wrap_content" android:id="@+id/btnExit"
    android:text="退出程序" android:layout_width="160dip"></Button>
    </LinearLayout>
    <ListView android:layout_width="fill_parent" android:id="@+id/lvDevices"
    android:layout_height="fill_parent">
    </ListView>
    </LinearLayout>

    主要就是两个嵌套<LinearLayout>和一个<ListView>。

    最后是主测试程序testBlueTooth.java:

    package com.testBlueTooth;
     
    import java.io.IOException;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.util.ArrayList;
    import java.util.List;
    //import java.util.UUID;
    import android.app.Activity;
    import android.bluetooth.BluetoothAdapter;
    import android.bluetooth.BluetoothDevice;
    import android.bluetooth.BluetoothSocket;
    import android.content.BroadcastReceiver;
    import android.content.ContentValues;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.net.Uri;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.ListView;
    import android.widget.Toast;
    import android.widget.ToggleButton;
     
    public class testBlueTooth extends Activity {
    //static final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";
    static final String FilePath = "file:///sdcard/test.jpg";
    Button btnSearch, btnDis, btnExit;
    ToggleButton tbtnSwitch;
    ListView lvBTDevices;
    ArrayAdapter<String> adtDevices;
    List<String> lstDevices = new ArrayList<String>();
    BluetoothAdapter btAdapt;
    public static BluetoothSocket btSocket;
     
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // Button 设置
    btnSearch = (Button) this.findViewById(R.id.btnSearch);
    btnSearch.setOnClickListener(new ClickEvent());
    btnExit = (Button) this.findViewById(R.id.btnExit);
    btnExit.setOnClickListener(new ClickEvent());
    btnDis = (Button) this.findViewById(R.id.btnDis);
    btnDis.setOnClickListener(new ClickEvent());
     
    // ToogleButton设置
    tbtnSwitch = (ToggleButton) this.findViewById(R.id.tbtnSwitch);
    tbtnSwitch.setOnClickListener(new ClickEvent());
     
    // ListView及其数据源 适配器
    lvBTDevices = (ListView) this.findViewById(R.id.lvDevices);
    adtDevices = new ArrayAdapter<String>(testBlueTooth.this, android.R.layout.simple_list_item_1, lstDevices);
    lvBTDevices.setAdapter(adtDevices);
    lvBTDevices.setOnItemClickListener(new ItemClickEvent());
     
    // 初始化本机蓝牙功能
    btAdapt = BluetoothAdapter.getDefaultAdapter();
     
    /*
    * 关于开关指示按钮,就是两装状态之间的切换:
    * 打开: 表示当前服务状态为开启,而不是在点击之后才开启
    * 关闭: 表示当前服务状态为关闭,而不是在点击之后才关闭
    * 如果理解反了,则下面的false/true设置也会反的!
    */

    if (btAdapt.getState() == BluetoothAdapter.STATE_OFF)// 读取蓝牙状态并显示
    tbtnSwitch.setChecked(false);
    else if (btAdapt.getState() == BluetoothAdapter.STATE_ON)
    tbtnSwitch.setChecked(true);
     
    // 注册Receiver来获取蓝牙设备相关的结果
    IntentFilter intent = new IntentFilter();
    intent.addAction(BluetoothDevice.ACTION_FOUND);// 用BroadcastReceiver来取得搜索结果
    intent.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
    intent.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
    intent.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    registerReceiver(searchDevices, intent);
    }
     
    /*
    * 搜索蓝牙设备列表
    */

    private BroadcastReceiver searchDevices = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    Bundle b = intent.getExtras();
    Object[] lstName = b.keySet().toArray();
     
    // 显示所有收到的消息及其细节
    for (int i = 0; i < lstName.length; i++) {
    String keyName = lstName[i].toString();
    Log.e(keyName, String.valueOf(b.get(keyName)));
    }
    //搜索设备时,取得设备的MAC地址
    if (BluetoothDevice.ACTION_FOUND.equals(action)) {
    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    String str= device.getName() + "|" + device.getAddress();
    // 防止重复添加
    if (lstDevices.indexOf(str) == -1){
    // 获取设备名称和mac地址
    lstDevices.add(str);
    }
    adtDevices.notifyDataSetChanged();
    }
    }
    };
     
    /*
    * 退出程序
    */

    protected void onDestroy() {
    this.unregisterReceiver(searchDevices);
    super.onDestroy();
    // 自毁进程
    android.os.Process.killProcess(android.os.Process.myPid());
    }
     
    /*
    * 想其它蓝牙设备发送一图片文件
    */

    class ItemClickEvent implements AdapterView.OnItemClickListener {
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    btAdapt.cancelDiscovery();
    String str = lstDevices.get(arg2);
    String[] values = str.split("\\|");
    String address=values[1];
    Log.e("address: ",values[1]);
     
    BluetoothDevice btDev = btAdapt.getRemoteDevice(address);
     
    try {
    /*
    * 网络流传的通过UUID连接的方式是不工作的,经过了好一阵搜索终于找到了下面的替换方法
    */

    //UUID uuid = UUID.fromString(SPP_UUID);
    //btSocket = btDev.createRfcommSocketToServiceRecord(uuid);
     
    Method m = null;
    try {
    m = btDev.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
    } catch (SecurityException e) {
    e.printStackTrace();
    } catch (NoSuchMethodException e) {
    e.printStackTrace();
    }
    try {
    btSocket = (BluetoothSocket) m.invoke(btDev, 1);
    } catch (IllegalArgumentException e) {
    e.printStackTrace();
    } catch (IllegalAccessException e) {
    e.printStackTrace();
    } catch (InvocationTargetException e) {
    e.printStackTrace();
    }
     
    btSocket.connect();
     
    /*
    * 该段落是通过btsocket发送字符串给其它蓝牙设备,接收端应该需要做相应接收处理才能收到
    * 所以尚未测试,暂且留着吧
    */

    // OutputStream outStream = null;
    // outStream = btSocket.getOutputStream();
    // String message = "Hello message from client to server.";
    // byte[] msgBuffer = message.getBytes();
    // try {
    // outStream.write(msgBuffer);
    // } catch (IOException e) {
    // e.printStackTrace();
    // }
    // btSocket.close();
     
     
    /*
    * 发送一指定的文件到其它蓝牙设备
    */

    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();
     
    /*
    * 下面一段是通过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);
     
     
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
     
     
    class ClickEvent implements View.OnClickListener {
    public void onClick(View v) {
    // 搜索蓝牙设备,在BroadcastReceiver显示结果
    if (v == btnSearch)
    {
    // 如果蓝牙还没开启,这提示开启
    if (btAdapt.getState() == BluetoothAdapter.STATE_OFF) {
    Toast.makeText(testBlueTooth.this, "请先打开蓝牙", 1000).show();
    return;
    }
    setTitle("本机蓝牙地址:" + btAdapt.getAddress());
    lstDevices.clear();
    btAdapt.startDiscovery();
    // 本机蓝牙启动/关闭
    } else if (v == tbtnSwitch) {
    if (tbtnSwitch.isChecked() == true)
    btAdapt.enable();
    else if (tbtnSwitch.isChecked() == false)
    btAdapt.disable();
    /*
    * 设置本机蓝牙可检测性,即可被他人搜索到。
    * 这里稍微说明一下:蓝牙设备一般都会支持“开启关闭”和“设置可见”两种行为:
    * 开启关闭:当然是打开/关闭蓝牙设备了
    * 设置可见:开启可检测性,以便其它蓝牙设备可以搜索到本机
    * 本人使用M9测试的,M9的“开启关闭”和“设置可见”默认被绑定了,即开启后可见属性也启用了,
    * 同样启用可见后(UI上不允许这么操作,通过下面的代码可以)蓝牙也开启了
    *
    * 所以为了避免重复操作,下面做了些判断:
    * 1. 如果是蓝牙开启,则不再需要启用可见
    * 2. 如果蓝牙未开启,这启用可见后,将蓝牙开启的指示按钮设置为开启状态
    *
    * 用于自己测试时应根据自己的手机情况稍做改动!
    * 如有的手机在设置蓝牙时需要对“开启关闭”和“设置可见“两个选项分别单独操作,
    * 这时只需要else里面部分就行了,即每次都执行设置可检测性操作。
    */

    } else if (v == btnDis)
    {
    if(tbtnSwitch.isChecked() == true){
    Toast.makeText(testBlueTooth.this, "蓝牙已经可见", 1000).show();
    }
    else{
    Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
    startActivity(discoverableIntent);
    tbtnSwitch.setChecked(true);
    }
    } else if (v == btnExit) {
    try {
    if (btSocket != null)
    btSocket.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    testBlueTooth.this.finish();
    }
    }
    }
    }

    里面都有详细的说明。

    注意:用户测试时需更改一下发送文件的路径,及FilePath变量的值!

    参考链接:
    [1] stackoverflow.com/questions/3397071/andr...ery-failed-exception
    [2] stackoverflow.com/questions/4921384/how-...roid-programatically
    [3] android.tgbus.com/Android/tutorial/201103/346657.shtml
    [4] www.mikenimer.com/?p=373

  • 相关阅读:
    洛谷1098 字符串的展开 解题报告
    洛谷1086 花生采摘 解题报告
    洛谷1125 笨小猴 解题报告
    洛谷1056 排座椅 解题报告
    洛谷1067 多项式输出 解题报告
    洛谷1003 铺地毯 解题报告
    JS在不同js文件中互相调用
    JS鼠标滚轮事件
    JS闭包
    PHP中header('content-type:text/html;charset="utf-8')和error_reporting()的作用
  • 原文地址:https://www.cnblogs.com/wzc0066/p/2948233.html
Copyright © 2020-2023  润新知