Android文件通过蓝牙共享

时间:2012-06-07 10:00:09

标签: android

我要求列出蓝牙上的所有可用设备,并将任何文件从Android设备共享到所选设备。我尝试使用以下代码

 private void sharefile(String deviceAddress,String filePath){
        File f =  new File(filePath);
        BluetoothAdapter objBluetoothAdapter = BluetoothAdapter
                        .getDefaultAdapter();

                BluetoothDevice device = objBluetoothAdapter
                        .getRemoteDevice(deviceAddress);
                Log.v("Device", "" + device.getName());

                final UUID uuid = UUID
                        .fromString("00001101-0000-1000-8000-00805F9B34FB");

                try {
                    byte[] buffer = new byte[(int) f.length()];
                    FileInputStream fis = new FileInputStream(f);
                    BufferedInputStream bis = new BufferedInputStream(fis);
                    bis.read(buffer, 0, (int) f.length());

                    BluetoothSocket socket = device
                            .createRfcommSocketToServiceRecord(uuid);

                    OutputStream os = socket.getOutputStream();
                    os.write(buffer, 0, buffer.length);
                    os.flush();
                    os.close();
                    socket.close();   
                } catch (SecurityException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

    }

执行上述代码时,Logcat上显示传输端点未连接消息。

06-11 18:26:49.960:W / System.err(9812):java.io.IOException:传输端点未连接 06-11 18:26:49.960:W / System.err(9812):在android.bluetooth.BluetoothSocket.writeNative(Native Method) 06-11 18:26:49.960:W / System.err(9812):在android.bluetooth.BluetoothSocket.write(BluetoothSocket.java:346) 06-11 18:26:49.960:W / System.err(9812):在android.bluetooth.BluetoothOutputStream.write(BluetoothOutputStream.java:85) 06-11 18:26:49.960:W / System.err(9812):at com.cit.bt.ShareMessageActivity.connectDevice(ShareMessageActivity.java:138) 06-11 18:26:49.960:W / System.err(9812):at com.cit.bt.ShareMessageActivity.shareFile(ShareMessageActivity.java:83) 06-11 18:26:49.960:W / System.err(9812):at com.cit.bt.ShareMessageActivity.onListItemClick(ShareMessageActivity.java:73) 06-11 18:26:49.960:W / System.err(9812):在android.app.ListActivity $ 2.onItemClick(ListActivity.java:319) 06-11 18:26:49.960:W / System.err(9812):在android.widget.AdapterView.performItemClick(AdapterView.java:284) 06-11 18:26:49.960:W / System.err(9812):在android.widget.ListView.performItemClick(ListView.java:3736) 06-11 18:26:49.960:W / System.err(9812):在android.widget.AbsListView $ PerformClick.run(AbsListView.java:1974) 06-11 18:26:49.960:W / System.err(9812):在android.os.Handler.handleCallback(Handler.java:587) 06-11 18:26:49.960:W / System.err(9812):在android.os.Handler.dispatchMessage(Handler.java:92) 06-11 18:26:49.960:W / System.err(9812):在android.os.Looper.loop(Looper.java:130) 06-11 18:26:49.960:W / System.err(9812):在android.app.ActivityThread.main(ActivityThread.java:3687) 06-11 18:26:49.960:W / System.err(9812):at java.lang.reflect.Method.invokeNative(Native Method) 06-11 18:26:49.960:W / System.err(9812):at java.lang.reflect.Method.invoke(Method.java:507) 06-11 18:26:49.960:W / System.err(9812):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:867) 06-11 18:26:49.960:W / System.err(9812):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 06-11 18:26:49.960:W / System.err(9812):at dalvik.system.NativeStart.main(Native Method)

并尝试使用以下替代代码连接设备,它连接设备但无法传输文件。

BluetoothDevice hxm = BluetoothAdapter.getDefaultAdapter()
                .getRemoteDevice(deviceAddress);
        Method m;

            m = hxm.getClass().getMethod("createRfcommSocket",
                    new Class[] { int.class });
            BluetoothSocket socket = (BluetoothSocket) m.invoke(hxm,
                    Integer.valueOf(1));
            socket.connect();

我也在android清单中提供了所需的权限,但它无法连接。 任何人都可以帮我解决这个问题。

你能否向我提供Android市场蓝牙文件传输背后的逻辑。

谢谢!

1 个答案:

答案 0 :(得分:0)

蓝牙HMX设备不会碰巧成为心率监测器吗? 无论哪种方式,我们都发布了一个很好的例子(源自Google蓝牙聊天示例)。它比他们的例子更加精简,希望你会发现它有用。

http://ww2.cs.fsu.edu/~sposaro/mobile/resources/BeatConnect.zip