WiFi P2P Communication(Already Connected Devices) - java

I try to make an app that send a message(String) to another app, I follow the official documentation : https://developer.android.com/guide/topics/connectivity/wifip2p but was too overwhelming because I'm a beginner.I found later a tutorial based on the official documentation but app doesn't work properly, the app crashed.Anyway , I don't want to carry the whole process for making a P2P connection, I just want to send that message for already connected device(manually) , I'm trying something like that but I don't know how:
If CONNECTED_DEVICES is equal to 1
sendMessage("Ana Are Mere");
Else If CONNECTED DEVICES is greater that 1
Toast : "Connect With Only A Device"
Else
Toast : "No Devices Connected"
It's not a very beautiful example, but that's my idea.

Related

How to connect with a Bluetooth device without pairing

I'm currently developping my first app in java.
This app requires a direct connection with my BL652, which doesn' t accept any kind of pairing option. That being said, I'm stuck with the code below, which still tries to pair with my bluetooth device instead of just connecting. Therefore, I wanted to know what am i supposed to do in order to make a connection that doesnt require pairing.
Thanks in advance for any response.
String mac_address = "DA:72:21:29:0F:F0";
private static final UUID MY_UUID = UUID.fromString("E54B0002-67F5-479E-8711-B3B99198CE6C");
lvNewDevices.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
BluetoothDevice device = (BluetoothDevice) mBluetoothAdapter.getRemoteDevice(mac_address);
try {
socket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
socket.connect();
} catch (IOException e) {
e.printStackTrace();
}
I'm not sure about connection to external hardware but it's definetly possible to connect 2 phones without pairing. I made it using Android Nearby Connections . And yeah it's basically working with BLE. As far as I know it's possible to connect to RaspberryPi using that API.
Bluetooth devices can be made to communicate with each other using master-slave configuration. I first tried with two BLE-HC05s, made one as master and the other as slave, and as expected the master is able to send data, slave is able to receive data, that can be seen using the Serial Monitor in Arduino IDE. I have developed a similar app, which connects to a BLE-HC05 device, sends and receives data. But since you are trying to get data or send data to the App on your phone, Android will not support such unpaired anonymous communications. If you are having trouble with connection or pairing Bluetooth device and your app, I may help you with that. But as far as Bluetooth communication without pairing is concerned, you may not be able to find a way.
Edit: The code to connect any bluetooth device, like BLE-HC06 as you have mentioned, is 1234 (Only if you have not changed it)

How to know if app user is offline android app?

Currently i am developing a cab booking app.
Now i am working on driver app.
Now my task is, i want to make him offline and online when ever internet get disconnected orconnected .
In my database i have a column is_online which value goes to 1 when he online and 0 when he is offline.
well now i know how to make him online but how to make him offline? if internet got disconnected ! how can i update value in db . it remain 1
never goes to 0;
Current i am using Broadcast Receiver that notify me when internet get disconnected or connected.
<?PHP
require_once 'include/DB_Settings_Functions.php';
$db = new DB_Functions();
// json response array
$response = array("error" => FALSE);
if(isset($_POST['driver_id'])&&isset($_POST['status']))
{
$driver_id= $_POST['driver_id'];
$status= $_POST['status'];
$user = $db->makeOnline($driver_id,$status);
}
?>
I heard that it can be solved by using mqtt any working example ? can anyone help me? i am using local host wamp server.
Edit:
In simple words i want to send value in server before connection goes down any idea how it can be done using mqtt ?
You can fire a request on every minutes from mobile app and stored it's date and time into data base. if user is online that means last access time will update every minutes in database and if user is offline then it's time last access time will not update in data base so you just need to fire a sql query for get offline user that have last access time is grater then 2 minus.
MQTT has a function called the Last Will and Testament (LWT), this is a messages that is published by the broker when the client connection times out.
This means that the client can publish a retained message to a unique topic when it comes online (e.g. publish 1 to /status). It can then set up a LWT to publish 0 to the same topic if the connection drops. It should also manually publish 0 before manually closing the connection as LWT only fires if the connection drop unexpectantly.
You can adjust the time out value to suit your needs.

Java desktop bluetooth client can not find services

I am new to bluetooth programming, so please keep that in mind.
I am trying to connect to a pulse sensor from a desktop pc, specifically the Zephyr HxM BT. I am not using any device specific drivers, the Java bluetooth library I'm using is Bluecove, and my bluetooth stack is BlueSoleil.
From my understanding, the way I proceed is
1) Scan for BT devices
public void startSearch() throws BluetoothStateException{
System.out.println("Inquiry started");
localdevice.getDiscoveryAgent().startInquiry(DiscoveryAgent.GIAC, this);
}
This works fine, it discovers my pulse belt and calls
public void deviceDiscovered(RemoteDevice arg0, DeviceClass arg1)
2) Search a device for services
Once a device has been discovered, it should be added to the DiscoveryAgent's list of cached devices, this is my first problem as the cache (and preknown devices) is always empty even though I've discovered my belt.
So the way that I do this now is to either keep my own list of devices, or simply start a service search directly from deviceDiscovered.
I am still a bit unsure if I'm using the correct parameters, but from reading the BT device manual and the javax.bluetooth documentation on DiscoveryAgent.searchServices:
public int searchServices(int[] attrSet,
UUID[] uuidSet,
RemoteDevice btDev,
DiscoveryListener discListener)
throws BluetoothStateException
My code:
public void searchServices(RemoteDevice device){
UUID[] uuidSet = new UUID[1];
uuidSet[0]=new UUID("1101",false); //Serial Port
System.out.println("Searching for services on " + device.getBluetoothAddress() );
try{
agent.searchServices(null,uuidSet, device,this);}
catch (BluetoothStateException e){
System.out.println("BluetoothStateException caught.");
}
}
I've set the attributes parameter to null, because the documentation states that this will have it search for services with the default attributes, however I've also tried to use only ServiceID (0x0003) as attribute with no luck.
This is where I'm stuck, I pass the correct BT device into the function, and it starts searching but never give me any results, it just searches forever for all I know, no exception, no calls to
public void servicesDiscovered(int arg0, ServiceRecord[] arg1)
or
public void serviceSearchCompleted(int arg0, int arg1)
So I guess my questions are:
- Am I doing something wrong? Any suggestions?
- What are the other approaches to connecting to a BT device, and what
information do I have to know about the device to do that?
Here is some information that I think is relevant from the device manual:
The following steps have to be undertaken to connect to a HxM device.
1) Activate the Bluetooth service of the device/computer wanting to connect to the HxM
2) Scan for Bluetooth devices in range
3) Pair with the HxM device found in range
4) Discover Services of Paired HxM
5) Connect to serial port of HxM device
The diagram above shows that the Bluetooth HxM typically communicates with a mobile device over the Bluetooth
link. The HxM only supports one link at a time and uses the Bluetooth SPP (Serial Port Profile) to communicate
with other devices with the following low-level protocol:
• 115,200 baud
• 8 data bits
• 1 stop bit
• No parity
Any suggestions are very much appreciated
edit: I just want to add that I'm testing the code with a console input loop, so the program is not immediately terminated after calling searchServices, it should have time to complete unless I'm misunderstanding async tasks
I just wanted to update this and say that I found the problem, it seems that I had to use a short UUID instead of a long. I should have tried both of these options before I deemed myself stuck, but I didn't think it would make any difference.

Bluetooth android server client with a headset

I'm trying to create a server on android that will listen on incoming connections ,such as a specific headset i have.
I've read many tutorials, posts in StackOverflow and the one from Android|Developer , and i don't seem to understand a few things .
1) UUID , is it a specific address for each Bluetooth device ? or is it a shared key that need to be in the server and the client in order to create a connection ? my guess is the latter cause there the MAC address as well ...
2) When i pair my Headset with my phone , does the headset saves the MAC\UUID of the last paired device ?
3) Does the Bluetooth chips even works in that way ? The phone connects immediately to a paired device as it turned on, So my guess is that it opens a Socket for each paired device and waits for it to turn on , is that true ?
4) Is it possible to accomplish what im trying ? Meaning creating a BluetoothServerSocket that will accept a connection from the head set ?
code example for the server side:
//This may b needs to be the UUID of the headset ? or special one ? or what ?
UUID myUUID = UUID.fromString("0000111e-0000-1000-8000-00805f9b34fb");
private final BluetoothServerSocket mServerSocket;
private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(Activity.APP_NAME, myUUID);
mServerScooket = tmp;
//Im hoping that this will block until the specific headset will be turned on
socket = mServerSocket.accept();
Thanks in advance.
1) UUID ,is a specific ID for Bluetooth communication. When you create your bluetooth socket in Android, listenUsingRfcommWithServiceRecord(Activity.APP_NAME, myUUID); ask Android to redirect bluetooth connection which match with the UUID to your app.
2) I'm not sure. It depends on the type of connection the headset can make. When you want to use it, do you need to select it in Android, or you can just put it and it works ?
3) I don't know. But you can have a look to Android source code ;)
4) If the bluetooth headset can create a connect socket, yes, it is possible
Hope this helps ;)
1) yes UUID is specific to each device and in order to connect to a device you need to have its UUID.
as shown in the Bluetooth chat example by Android SDK
2) depends on the hardware for example
chip sets like the HC-06 does saves the last paired device UUID
while the HC-05 dose not ...
3) The headsets that Ive tested (Samsung made) Acts as a server.
so by initiating a BluetoothSocket with the UUID of the headphone u can connect to it.
4) Yes it is possible to connect to a Bluetooth head set answered in the post : Using the Android RecognizerIntent with a bluetooth headset
Special notes :
Best way to listen to oncoming connections without any wakelocks that I found is by registering the BluetoothDevice.ACTION_ACL_CONNECTED Broadcast and check the name\mac\UUID of each incomming connection.
Thanks to Hoan Nagayu for the help.

Playing live http stream in vlcj

I'm trying to use vlcj to play live internet radio stations in a project. I've played around with some sample programs for a few hours, but I cannot get either the sample programs or programs that I've played around with to play the stream from the URL.
An example of a URL I'm trying to play is: http://network.absoluteradio.co.uk/core/audio/wmp/live.asx?service=vr
Is there anything special I have to do in order to get vlcj to play this stream? I couldn't find anything to help in the API. (Assuming it can because it can be played through the VLC media player!)
Thanks a lot
Ok, the MRL you have provided us http://network.absoluteradio.co.uk/core/audio/wmp/live.asx?service=vr is a MMS server that may pull a ASX (XML) metafile which may contain at least one sub-item.
http://all-streaming-media.com/faq/streaming-media/Metafiles-ASX-Advanced-Stream-Redirector.htm
To be able to play this type of streaming media and go through each sub-item, you need to do the following code snippet:
VideoPanel.getMediaPlayer().setRepeat(true);
VideoPanel.getMediaPlayer().setPlaySubItems(true);
VideoPanel.getMediaPlayer().prepareMedia(media, options);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for(String s: VideoPanel.getMediaPlayer().subItems()) System.out.println(s);
VideoPanel.getMediaPlayer().play();
For the example MRL above, it will list down all sub-items as follows:
http://wms.absoluteradio.co.uk/g1/absoluteradio.co.uk/prerolls/ar_account_1310455302_hi.wma
mms://wms.absoluteradio.co.uk/absoluteradio.co.uk/vr_lo?u=
http://wms.absoluteradio.co.uk/absoluteradio.co.uk/vr_lo?u=
mmsu://wms.absoluteradio.co.uk/absoluteradio.co.uk/vr_lo?u=
mmst://wms.absoluteradio.co.uk/absoluteradio.co.uk/vr_lo?u=
mms://wms.absoluteradio.co.uk/absoluteradio.co.uk/prerolls/problems_lo.wma
To stop playing all of them, set the following code snippet:
VideoPanel.getMediaPlayer().setRepeat(false);
VideoPanel.getMediaPlayer().setPlaySubItems(false);
VideoPanel.getMediaPlayer().stop();
For a better explanation, refer to: http://code.google.com/p/vlcj/wiki/HowToHandleYouTubeMedia
You cannot use the http to play such link directly. You will ve to use the port number of the radio station router. this is because if i want to receive my home live video streaming from the internet at my workplace, i type the following on url: http://my dns server ip address:8080 The 8080 is the port number I opened on my router.

Categories

Resources