Android的蓝牙文件发送蓝牙、文件、Android

由网友(赐我梦境)分享简介:我想在Android设备发送文件通过蓝牙。我曾经做过的发现,连接,并都取得了蓝牙接口。当我写了蓝牙接口的输出流的字节数组问题是,在recieving方没有收到任何东西,尽管它接受的东西被发送。下面就是这样做的IAM(坏的蓝牙适配器)请指教。尝试{BluetoothDevice类开发= bad.getRemoteDevi...

我想在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();
    }
 
Android手机蓝牙传送文件详细教程

解决方案

我使用的是下面的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();

阅读全文

相关推荐

最新文章