Android的蓝牙文件发送 [英] Android Bluetooth file sending

查看:211
本文介绍了Android的蓝牙文件发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Android设备发送文件通过蓝牙。我曾经做过的发现,连接,并都取得了蓝牙接口。当我写了蓝牙接口的输出流的字节数组问题是,在recieving方没有收到任何东西,尽管它接受的东西被发送。

下面就是这样做的IAM(坏的蓝牙适配器)

请指教。

 尝试
    {
        BluetoothDevice类开发= bad.getRemoteDevice(一);
        bad.cancelDiscovery();

        dev.createRfcommSocketToServiceRecord(新的UUID(1111,2222));
        方法M = dev.getClass()实现getMethod(createRfcommSocket,新的等级[] {} int.class)。
        BS =(的BluetoothSocket)m.invoke(dev的,Integer.valueOf(1));
        bs.connect();
        tmpOut = bs.getOutputStream();
    }赶上(例外五)
    {

    }

    文件F =新的文件(文件名);

    BYTE B [] =新的字节[(INT)f.length()];
    尝试
    {
        的FileInputStream的FileInputStream =新的FileInputStream(F);
        fileInputStream.read(B);
    }赶上(IOException异常E)
    {
        Log.d(TAG,错误转换文件);
        Log.d(TAG,e.getMessage());
    }

    尝试 {
        tmpOut.write(B);
    }赶上(IOException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }
 

解决方案

我使用的是下面的code剪断连接到串行服务远程蓝牙设备,这是工作对我罚款。只要确保其他设备(可以是手机或PC)有一个服务器插槽,串口通信通过蓝牙(见服务器端$ C $低于C)

客户端:

  UUID serialUUID = UUID.fromString(00001101-0000-1000-8000-00805F9B34FB);
BluetoothDevice类btDevice = btAdapter.getRemoteDevice(BTAddress); //获取后再扫描BTAddress
的BluetoothSocket btSocket = btDevice.createRfcommSocketToServiceRecord(SERIAL_UUID);
btSocket.connect();
InputStream中的IStream = btSocket.getInputStream();
的OutputStream的ostream = btSocket.getOutputStream();
 

服务器端:

  UUID serialUUID =新的UUID(1101,真正的);
字符串的serviceURL =btspp://本地主机:+ serialUUID
        +;名称= Android的BT服务器,授权= FALSE;认证=假;
StreamConnectionNotifier connectionNotifier =(StreamConnectionNotifier)连接器
                        。开(serviceURL中);
//阻塞的方法将等待客户端连接
的StreamConnection连接= connectionNotifier.acceptAndOpen();

远端设备远端设备= RemoteDevice.getRemoteDevice(连接);
InputStream的btInput = connection.openInputStream();
OutputStream的btOutput = connection.openOutputStream();
 

I am trying to send a file over bluetooth in an android device. I have done discovery, connection and have made a bluetooth socket. Problem is when i am writing the byte array in the output stream of the bluetooth socket, the recieving side does not receive anything although it accept that something is being sent.

Here's what Iam doing (bad is the bluetooth adaptor)

Please advise.

try
    {
        BluetoothDevice dev = bad.getRemoteDevice(a);
        bad.cancelDiscovery();

        dev.createRfcommSocketToServiceRecord(new UUID(1111, 2222));
        Method m = dev.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
        bs = (BluetoothSocket) m.invoke(dev, Integer.valueOf(1));
        bs.connect();
        tmpOut = bs.getOutputStream();
    }catch(Exception e)
    {

    }

    File f = new File(filename);

    byte b[] = new byte[(int) f.length()];
    try
    {
        FileInputStream fileInputStream = new FileInputStream(f);
        fileInputStream.read(b);
    }catch(IOException e)
    {
        Log.d(TAG, "Error converting file");
        Log.d(TAG, e.getMessage());
    }

    try {
        tmpOut.write(b);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

解决方案

I am using the below code snipped to connect to the serial service in a remote Bluetooth device and it is working fine for me. Just make sure that the other device (can be mobile or PC) has a server socket for serial communication over Bluetooth (see the server side code below)

Client Side:

UUID serialUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
BluetoothDevice btDevice = btAdapter.getRemoteDevice(BTAddress); // Get the BTAddress after scan
BluetoothSocket btSocket = btDevice.createRfcommSocketToServiceRecord(SERIAL_UUID);
btSocket.connect();
InputStream iStream = btSocket.getInputStream();
OutputStream oStream = btSocket.getOutputStream();

Server Side:

UUID serialUUID = new UUID("1101", true);
String serviceURL = "btspp://localhost:" + serialUUID
        + ";name=Android BT Server;authorize=false;authenticate=false";
StreamConnectionNotifier connectionNotifier = (StreamConnectionNotifier) Connector
                        .open(serviceURL);
// Blocking method will wait for client to connect
StreamConnection connection = connectionNotifier.acceptAndOpen();

RemoteDevice remoteDevice = RemoteDevice.getRemoteDevice(connection);
InputStream btInput = connection.openInputStream();
OutputStream btOutput = connection.openOutputStream();

这篇关于Android的蓝牙文件发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆