Sending an APK via Bluetooth to another device - android

I´m trying to develop an App which can send an .APK file via Bluetooth to another device with an Insecure Rfcomm connection. I´m running self-coded apps on both devices. The apps are not the same but I can still set the UUID, Rfcomm Listener and so on, so the connection should work fine and because of the insecure rfcomm without any alert to accept the transfer. I´ve tried the BluetoothChat example from the Android developer page, but I´ve got problems with changing the code for my purposes. Can anyone give me some snippets of code or any link to an tutorial how to setup Apps for Bluetooth File transfer? Every useful stuff is welcome.
EDIT:
You could say I´m trying to develop an app, which can "update" another app on another phone by Bluetooth.
Thanks

This worked for me:
public void shareApk(Context context) {
try {
PackageManager pm = context.getPackageManager();
ApplicationInfo ai = pm.getApplicationInfo(context.getPackageName(), 0);//context.getPackageName() is used for send my app's apk, you can give package name which you want to share
File srcFile = new File(ai.sourceDir);
Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
share.setType("application/vnd.android.package-archive");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(srcFile));
share.setPackage("com.android.bluetooth");
context.startActivity(share);
//context.startActivity(Intent.createChooser(share, context.getString(R.string.share_using)));
} catch (Exception e) {
Log.e("ShareApp", e.getMessage());
}
}

Related

Android FTP Intent

I am developing an app which transfers files to and from Android Wear watch by using FTP server running on watch. Right now I connect devices to same network by creating hotspot on smartphone. Then FTP server is started on watch and smartphone can connect to it. The problem is that I want to create som simple and universal app to connect from smartphone. However hotspot uses DHCP and I cant asign static IP to watch so I want to create only small app for smartphone which creates something like FTP intent and then some file explorer should open this intent. For example I prepare intent lik:
String url="ftp://user:pass#192.168.43.5:21"
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
but I am getting no activity to handle intent found exception with es file explorer and also solid file explorer installed. I have tried using AndFTP which provides very good intent documentation but it wasn't very stable. Do you have any idea if ES file explorer or something similar can accept intent to open ftp connection?
I don't know about ES File Explorer or any other app but I found it could be done using chrome with a little hack. By using google chrome's navigator.
String url = "ftp://user:pass#192.168.43.5:21";
String uri = "googlechrome://navigate?url=" + url;
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(uri));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setPackage("com.android.chrome");
The last 2 lines are to open intent specifically in chrome.
I hope this solves your problem.

Send file via bluetooth to Android device that doesnt have the app installed

I am looking for viable ways to send a file from my android app to another android device that doesnt have my app installed.
Option 1. Via share intent: https://stackoverflow.com/a/16755810/1865583
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse(picURI);
sharingIntent.setType("image/*");
sharingIntent.setPackage("com.android.bluetooth");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image"));
Issue regarding option 1:
I would love if i could specify the device i want to send this file to, in the intent parameters? I've searched online but cant find this option.
I've stumbled across this multiple times but it only works on particular devices as far people claim. https://evilzone.org/java/%28android%29-three-ways-to-send-data-over-bluetooth/
ContentValues values = new ContentValues();
String address = device.getAddress();
values.put(BluetoothShare.URI, Uri.fromFile(new File("somefile.mp3")).toString());
values.put(BluetoothShare.DESTINATION, address);
values.put(BluetoothShare.DIRECTION, BluetoothShare.DIRECTION_OUTBOUND);
Long ts = System.currentTimeMillis();
values.put(BluetoothShare.TIMESTAMP, ts);
Uri contentUri = getContentResolver().insert(BluetoothShare.CONTENT_URI, values);
Is there a working/tested way to specify the device the bluetooth will try to send the file too? (and pair by default if necessary)?
Option 2. Using Bluetooth sockets to control the file transfer :
I am actually quite interested in developing this options, but all the examples online go about sending files from client to server where both code bases are developed by me.
What i need is to connect via bluetooth to the other android bluetooth enabled device and initiate the file transfer? But i am unsure how to initiate this transfer or which protocols are used if any etc? How would you connect to the default bluetooth app to send it a file?
I've found some information that i thought using as starting point https://stackoverflow.com/a/17237945/1865583
The user talks about using 00001106-0000-1000-8000-00805F9B34FB (File transfer service) and starting a RfcommSocket for communication. Would this be the right way to go to communicate with bluetooth android apps for file transfer?

How to receive files programmatically using bluetooth

I am trying to receive data (strings) from a Bluetooth enabled device whose MAC_ID is known.
I have searched many examples ,but each article is pointing to Bluetooth Chat example, I think in Bluetooth Chat example, application need to be installed on both the devices for them to be connected and exchange strings.Correct me if I am wrong.
But I need to install application only on Receiver device.I have tried installing the application only on one device and tried connecting to the sender device, without success.
Bluetooth is a peer to peer protocol where you need to have application running on both sides. Hence if you want to exchange data very good example would be Bluetooth chat. If you want to download or transfer a file you should either implement obex or FTP profile based applications.
Yes you need to deploy an application on both sides. If you are really restricted in a way that you only can deploy on one side, you have to figure out which standard protocols/bluetooth profiles the other side is capable of. You can figure that out by doing a SDP lookup. For a device, you will then get a list of UUIDs identifying these services. See the bluetooth spec for well known UUIDs. As #7383 pointed out, your are most probably looking for OBEX or FTP.
If you can deploy on both sides, you can write your own app using Blaubot (disclaimer: I wrote it). A simple Blaubot program would do this:
UUID MY_UUID = UUID.fromString("33bb1246-1472-11e5-b60b-1697f925ec7b");
// onCreate() or in a service, we create a blaubot instance
// using Bluetooth to form a network and Bluetooth + NFC to find devices
IBlaubotDevice ownDevice = new BlaubotDevice();
BlaubotUUIDSet uuidSet = new BlaubotUUIDSet(MY_UUID);
BlaubotBluetoothAdapter bluetoothAdapter = new BlaubotBluetoothAdapter(uuidSet, ownDevice);
BlaubotNFCBeacon nfcBeacon = new BlaubotNFCBeacon();
BlaubotBluetoothBeacon bluetoothBeacon = new BlaubotBluetoothBeacon();
this.mBlaubot = BlaubotAndroidFactory.createBlaubot(MY_UUID, ownDevice, adapter, nfcBeacon, bluetoothBeacon);
// start and wait until connected
this.mBlaubot.startBlaubot();
// create a channel and send your file
IBlaubotChannel fileChannel = this.mBlaubot.createChannel(1);
// convert your file to its bytes
File yourFile = // ... however you get it
byte[] fileBytes = ...// ... see http://stackoverflow.com/questions/858980/file-to-byte-in-java
// send it to all connected devices
fileChannel.publish(fileBytes, true);
// to receive it on the other device, do this:
// subscribe to the channel
fileChannel.subscribe(new IBlaubotMessageListener() {
#Override
public void onMessage(BlaubotMessage message) {
// extract your bytes from the message
byte[] fileBytes = message.getPayload();
// .. do something useful or write it to a file again
// to write it to a file
File file = new File(yourFilePath);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
bos.write(fileBytes);
bos.flush();
bos.close();
}
});
This should be all you need. To allow the devices to connect, you have to pair them before or use NFC (just hold them together, when Blaubot is started). If you go with Blaubot let me know if you have problems that can't be solved with the documentation or the android quickstart guide.
I can only guess what your actual scenario looks like. If you have two android phones, this should work. If that is not the case, you should add more informations about the involved devices. Are we really talking about (classic) Bluetooth connections or are you trying to get data from a Bluetooth Low Energy device?
In this case the famous chat example will not help you either.

How can I programmatically connect and disconnect vpn connections by using android's "openvpn connect" app in combination with intents?

I'm working on an android app that should start an OpenVPN Connect session automatically when needed.
How can I programmatically connect and disconnect vpn connections by using android's "openvpn connect" app in combination with intents ?
Edit:
Meanwhile I found this approach - it works for me:
private void startVPN() {
Intent openVPN = new Intent("android.intent.action.VIEW");
openVPN.setPackage("net.openvpn.openvpn");
openVPN.setClassName("net.openvpn.openvpn", net.openvpn.openvpn.OpenVPNClient");
openVPN.putExtra("net.openvpn.openvpn.AUTOSTART_PROFILE_NAME", "10.10.10.10 [profilename]");
startActivityForResult(openVPN, 0);
}
This starts the "OpenVPN Connect" app and uses the profilename to do an auto-connect.
If successfull the app goes to background by itself.
Is there even a way to do this completly in background ?
Stopping the VPN-connection does everything in background.
private void stopVPN() {
Intent openVPN = new Intent("android.intent.action.VIEW");
openVPN.setPackage("net.openvpn.openvpn");
openVPN.setClassName("net.openvpn.openvpn", "net.openvpn.openvpn.OpenVPNDisconnect");
startActivityForResult(openVPN, 0);
}
OpenVPN's official Android client can be invoked through AIDL. There's an entire sample app available, with source code. It even has a relatively friendly license.

sending file using built in bluetooth application in android

I am sending file using following code in my android application :
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/png");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file) );
startActivity(intent);
But it shows me list like email , bluetooth , and many other. I dont want this list and should be sent directly without user interaction to perticular paired device .
Is it possible in android ?
Thanks in advance.
Nope, current API's will not allow you to bypass user confirmation, but you can do this programmatically using the Bluetooth API's after the pairing stage. Establish a RFCOMM and then use InputStream/OutputStream to receive/send files. A great place to start is looking at the BluetoothChat example by google.

Categories

Resources