You can use the obex library. It seemed that android did not provide the obex library, but I solved the problem and the solution is posted here .
Further explanation (please start reading from here if you are busy)
- I tried to create a remote Android phone controller (and something similar to a telnet server), which helps to remotely control the phone using my old functional phone.
Primary Content: Bluetooth FTP Client
My first plan was to make the application check the list of files in my phone book.
But I did not know how to connect to the ftp server of my functions.
I talked a lot about how to connect to the ftp server via bluetooth, but I could only find that the Bluetoorh FTP server was using OBEX Protocol .
I found useful material (a PDF file) in the SO stream and studied OBEX connect, put and get requests.
So, I finally wrote some codes that try to connect to the Bluetooth FTP server. I want to show them to you, but I lost them: (The codes were like just writing out sequences of bytes into the output stream.
It was also difficult for me to find out that the UUID makes the application connected to the FTP client. But I tried all the UUIDs obtained using the code below.
String parcels=""; ParcelUuid[] uuids=mBtDevice.getUuids(); int i=0; for (ParcelUuid p:uuids) { parcels += "UUID UUID" + new Integer(i).toString() + "=UUID.fromString((\"" + p.getUuid().toString() + "\"));\n\n"; ++i; }
Nothing seemed to bring me to the answer I wanted. So I searched googled more and found out that I should not only use the UUID 00001106-0000-1000-8000-00805f9b34fb to connect to the OBEX FTP server, but also to transfer the target header ** with UUID ** F9EC7BC4-953C- 11D2-984E-525400DC9E09 when sending an OBEX connect request. The code below shows how to connect to the bluetooth FTP server as a client.
try { mBtSocket = mBtDevice.createInsecureRfcommSocketToServiceRecord(UUID.fromString(" 00001106-0000-1000-8000-00805f9b34fb")); } catch (Exception e) { //e.printStackTrace(); } Thread thread=new Thread(new Runnable() { public void run() { UUID uuid=UUID.fromString("F9EC7BC4-953C-11D2-984E-525400DC9E09"); ByteBuffer bb = ByteBuffer.wrap(new byte[16]); bb.putLong(uuid.getMostSignificantBits()); bb.putLong(uuid.getLeastSignificantBits()); byte [] bytes=bb.array(); Operation putOperation=null; Operation getOperation=null; try { // connect the socket mBtSocket.connect(); //I will explain below mSession = new ClientSession((ObexTransport)(mTransport = new BluetoothObexTransport(mBtSocket))); HeaderSet headerset = new HeaderSet(); headerset.setHeader(HeaderSet.TARGET, bytes); headerset = mSession.connect(headerset); if (headerset.getResponseCode() == ResponseCodes.OBEX_HTTP_OK) { mConnected = true; } else { mSession.disconnect(headerset); } ...
Then you are connected as an FTP client and are ready to use OBEX operations to send files, request files, list directories, etc.
- However, I did not want to wait an hour to send my team to my Android phone. (And that would be ineffective if I increased the polling frequency, since each polling method.)
Start reading here if you are busy. Main Content: OBEX OPP
For the reason I mentioned above, I eagerly searched for the OPP manipulation methods that I found from the OBEX documentation.
You might want to transfer files via bluetooth in normal mode (without specifying your protocol and creating a new desktop application for it) on your computer, right? Then sending the Inbox service that runs on your Windows computer to OBbox is the best solution. So, how can we connect to the OPP (Obex Object Push) inbox?
- Setting up the OBEX library Add
import javax.obex; into the source code. If your compiler does not support the OBEX library, download the sources and add to your project here . - Implementing
ObexTransport You must provide a class that implements ObexTransport in the library when using it. It defines how the library should send data (e.g. RFCOMM, TCP, ...). An example implementation is here . This can lead to some runtime or compilation errors, such as there no method . But you can partially fix this by replacing method calls with constants such as return 4096 instead of return mSocket.getMaxTransmitPacketSize(); by removing the if statements of the public int getMaxTransmitPacketSize() . Or you can try using reflection to get these execution methods. - Get
BluetoothSocket Get the bluetooth connector using mBtDevice.createInsecureRfcommSocketToServiceRecord(UUID.fromString(" 00001105-0000-1000-8000-00805f9b34fb" )); And call connect() . - Create
ClientSession Create an instance of your ObexTransport implementation and create a new ClientSession , for example mSession = new ClientSession((ObexTransport)(mTransport = new BluetoothObexTransport(mBtSocket))); . Send the OBEX connect request to your computer. Inbound OPP Services.
HeaderSet headerset = new HeaderSet(); // headerset.setHeader(HeaderSet.COUNT,n); headerset = mSession.connect(null); if (headerset.getResponseCode() == ResponseCodes.OBEX_HTTP_OK) { mConnected = true; }
Submit OBEX hosting requests using ClientSession .
protected boolean Put(ClientSession session, byte[] bytes, String as, String type) { // TODO: Implement this method //byte [] bytes; String filename=as; boolean retry=true; int times=0; while (retry && times < 4) { Operation putOperation=null; OutputStream mOutput = null; //ClientSession mSession = null; //ArrayUtils.reverse(bytes); try { // Send a file with meta data to the server final HeaderSet hs = new HeaderSet(); hs.setHeader(HeaderSet.NAME, filename); hs.setHeader(HeaderSet.TYPE, type); hs.setHeader(HeaderSet.LENGTH, new Long((long)bytes.length)); Log.v(TAG,filename); //Log.v(TAG,type); Log.v(TAG,bytes.toString()); putOperation = session.put(hs); mOutput = putOperation.openOutputStream(); mOutput.write(bytes); mOutput.close(); putOperation.close(); } catch (Exception e) { Log.e(TAG, "put failed", e); retry = true; times++; continue; //e.printStackTrace(); } finally { try { if(mOutput!=null) mOutput.close(); if(putOperation!=null) putOperation.close(); } catch (Exception e) { Log.e(TAG, "put finally" , e); retry = true; times++; continue; } //updateStatus("[CLIENT] Connection Closed"); } retry = false; return true; } return false; }
Finally, unplug.
private void FinishBatch(ClientSession mSession) throws IOException { mSession.disconnect(null); try { Thread.sleep((long)500); } catch (InterruptedException e) {} mBtSocket.close(); }
Then here is the shell class.
import android.bluetooth.*; import android.util.*; import java.io.*; import java.util.*; import javax.obex.*; public class BluetoothOPPHelper { String address; BluetoothAdapter mBtadapter; BluetoothDevice device; ClientSession session; BluetoothSocket mBtSocket; protected final UUID OPPUUID=UUID.fromString(("00001105-0000-1000-8000-00805f9b34fb")); private String TAG="BluetoothOPPHelper"; public BluetoothOPPHelper(String address) { mBtadapter=BluetoothAdapter.getDefaultAdapter(); device=mBtadapter.getRemoteDevice(address); try { mBtSocket = device.createRfcommSocketToServiceRecord(OPPUUID); } catch (IOException e) { throw new RuntimeException(e); } } public ClientSession StartBatch(int n) { ClientSession mSession = null; // TODO: Implement this method boolean retry=true; int times=0; while (retry && times < 4) { //BluetoothConnector.BluetoothSocketWrapper bttmp=null; try { mBtSocket.connect(); //bttmp = (new BluetoothConnector(device,false,BluetoothAdapter.getDefaultAdapter(),Arrays.asList(new UUID[]{OPPUUID,OPPUUID, OPPUUID}))).connect();//*/ device.createInsecureRfcommSocketToServiceRecord(OPPUUID); /*if(mBtSocket.isConnected()) { mBtSocket.close(); }*/ } catch (Exception e) { Log.e(TAG, "opp fail sock " + e.getMessage()); retry = true; times++; continue; } try { //mBtSocket=bttmp.getUnderlyingSocket(); // mBtSocket.connect(); BluetoothObexTransport mTransport = null; mSession = new ClientSession((ObexTransport)(mTransport = new BluetoothObexTransport(mBtSocket))); HeaderSet headerset = new HeaderSet(); // headerset.setHeader(HeaderSet.COUNT,n); headerset = mSession.connect(null); if (headerset.getResponseCode() == ResponseCodes.OBEX_HTTP_OK) { boolean mConnected = true; } else { Log.e(TAG, "SEnd by OPP denied;"); mSession.disconnect(headerset); times++; continue; } } catch (Exception e) { Log.e(TAG, "opp failed;" , e); retry = true; times++; continue; //e.rintStackTrace(); } retry=false; } return mSession; } protected boolean Put(ClientSession session, byte[] bytes, String as, String type) { // TODO: Implement this method //byte [] bytes; String filename=as; boolean retry=true; int times=0; while (retry && times < 4) { Operation putOperation=null; OutputStream mOutput = null; //ClientSession mSession = null; //ArrayUtils.reverse(bytes); try { // Send a file with meta data to the server final HeaderSet hs = new HeaderSet(); hs.setHeader(HeaderSet.NAME, filename); hs.setHeader(HeaderSet.TYPE, type); hs.setHeader(HeaderSet.LENGTH, new Long((long)bytes.length)); Log.v(TAG,filename); //Log.v(TAG,type); Log.v(TAG,bytes.toString()); putOperation = session.put(hs); mOutput = putOperation.openOutputStream(); mOutput.write(bytes); mOutput.close(); putOperation.close(); } catch (Exception e) { Log.e(TAG, "put failed", e); retry = true; times++; continue; //e.printStackTrace(); } finally { try { if(mOutput!=null) mOutput.close(); if(putOperation!=null) putOperation.close(); } catch (Exception e) { Log.e(TAG, "put finally" , e); retry = true; times++; continue; } //updateStatus("[CLIENT] Connection Closed"); } retry = false; return true; } return false; } protected boolean Put(ClientSession s, OPPBatchInfo info) { return Put(s,info.data,info.as,info.type); } private void FinishBatch(ClientSession mSession) throws IOException { mSession.disconnect(null); try { Thread.sleep((long)500); } catch (InterruptedException e) {} mBtSocket.close(); } public boolean flush() throws IOException { if (sendQueue.isEmpty()) { return true; } try { Thread.sleep((long)2000); } catch (InterruptedException e) {} ClientSession session=StartBatch(sendQueue.size()); if (session == null) { return false; } while (!sendQueue.isEmpty()) { if (Put(session, sendQueue.remove()) == false) { Log.e(TAG, "Put failed"); } } FinishBatch(session); return true; } Queue<OPPBatchInfo> sendQueue; public boolean AddTransfer(String as,String mimetype,byte[] data) { return sendQueue.add(new OPPBatchInfo(as,mimetype,data)); } class OPPBatchInfo { String as; String type; byte[] data; public OPPBatchInfo(String as,String type,byte[] data) { this.as=as; this.data=data; this.type=type; } }
}
KYHSGeekCode
source share