通过蓝牙将数据从Android传输到PC

时间:2013-02-11 13:12:36

标签: android bluetooth client-server data-transfer

任何人都可以提供一个蓝牙服务器客户端(从android到计算机)的工作示例,它可以将文件或消息从一端传输到另一端?我使用TCP制作它,但我最近2天无法成功使用蓝牙。
我发现了谷歌的一些文章,但我无法用这些文章取得成功。从This教程我自己尝试,但在 onResume()中出现连接失败的例外情况。
我想将数据从我的 android手机传输到我的PC 运行Windows 7.现在我使用下面的代码工作正常,因为从logcat我看到连接设置成功并完美读取数据,但仍然没有将数据传输到我的电脑(可能无法写入这些数据) 所以我的问题是,我是否遗漏了某些东西或需要这样的服务器端代码?或者任何人都可以建议一些代码应该成功将消息或文件从客户端android传输到服务器PC ?<登记/> 我的代码:

Button btnSend = null;
TextView txtPath = null;
Socket s = null;
BluetoothAdapter objBluetoothAdapter = null;
BluetoothDevice device = null;
BluetoothSocket socket = null;
String strPath = "/sdcard/bluetooth/IMG0245A.jpg";
byte [] buffer = null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btnSend = (Button)findViewById(R.id.send_button);
    btnSend.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
        // TODO Auto-generated method stub

        String address="MY_COMPUTER_BLUETOOTH_ADDRESS";

        objBluetoothAdapter =  BluetoothAdapter.getDefaultAdapter();
        if(objBluetoothAdapter==null){
                Toast.makeText(this, "BT not supported", Toast.LENGTH_LONG);
                return;
        }

        //objBluetoothAdapter.enable();

        if(!objBluetoothAdapter.isEnabled()){
                Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivity(enableBT);
        }
        try{
                device = objBluetoothAdapter.getRemoteDevice(address);

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

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

                        socket = device.createRfcommSocketToServiceRecord(uuid);
                        Log.d("BT","RF Connection Created"+socket);
                        //objBluetoothAdapter.startDiscovery();
                        for(int i=0;i<3;i++){
                                try{

                                        objBluetoothAdapter.cancelDiscovery();
                                        socket.connect();
                                        Log.d("BT","Socket Connected = "+socket);

                                        break;
                                }catch (Exception e) {
                                        // TODO: handle exception
                                        Log.d("BT","Socket Connection exception = "+e);
                                }
                        }
                }catch (Exception e) {
                        // TODO: handle exception
                        e.printStackTrace();
                        Log.d("BT","Connection NOT OK");
                }

                OutputStream os = socket.getOutputStream();
                os.write(buffer);//,0,buffer.length);
                os.flush();
                os.close();
                socket.close();

        } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Toast.makeText(this,"exception "+e, Toast.LENGTH_LONG);
        }
}

1 个答案:

答案 0 :(得分:1)

您是否看过SDK中提供的示例?如果没有,你会发现一个名为“蓝牙聊天”的。它为您需要的一切提供基础。我前段时间用它来做你想做的事情。撕掉你不需要的东西并添加你做的事情,然后你就参加了比赛。

我会提供示例代码,除了我不再拥有它,抱歉。无论如何,你会发现蓝牙聊天是一个非常完整的例子。

相关问题