I've built and ran the RemoteDeviceDiscovery sample project for BlueCove 2.1.1. The program will discover all discoverable bluetooth devices but it will also report in deviceDiscovered() devices that were once paired before.
Is there a way to ONLY discover devices around you (like in Android)? The code below returns BOTH discovered devices and paired devices...
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
System.out.println("Device " + btDevice.getBluetoothAddress() + " found");
devicesDiscovered.addElement(btDevice);
try {
System.out.println(" name " + btDevice.getFriendlyName(false));
} catch (IOException cantGetDeviceName) {
}
}
Yes - use http://bluecove.org/bluecove/apidocs/javax/bluetooth/RemoteDevice.html#isTrustedDevice() to filter unpaired devices (or paired devices if you wish to).
Related
I am writing a program in Java for android. When calling the getSequencer method, I get a MidiUnavailableException exception.
When executing this code :
MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo();
if (devices.length == 0) {
System.out.println("No MIDI devices found");
} else {
for (MidiDevice.Info dev : devices) {
System.out.println(dev);
}
}
I got the "No MIDI devices found". I realized that you can solve this problem by installing soundbank at this link:https://www.oracle.com/java/technologies/sound-banks.html
But it describes the installation method for Windows. And how to install soundbank for android?
followed the instruction as stated here . Added the properties file in my root project and libraries on the project class path. When i run the project, it returns.
Exception in thread "main" javax.usb.UsbPlatformException: Class org.usb4java.javax.Services does not have the needed constructor
at javax.usb.UsbHostManager.initialize(UsbHostManager.java:46)
at javax.usb.UsbHostManager.getUsbServices(UsbHostManager.java:24)
at usbfinderdemo.UsbFinderDemo.main(UsbFinderDemo.java:30)
Don't know what might be wrong. Thinking i might not be using the right .jar file of usb4java, but i'm not certain yet as the code does not show any error at all.
Code Snippet
UsbServices services = UsbHostManager.getUsbServices();//the line that throws the error.
UsbHub rootHub = services.getRootUsbHub();
List<UsbDevice> devices = rootHub.getAttachedUsbDevices();
if (devices.size() > 0) {
System.out.println("USB devices found.");
} else {
System.out.println("No USB devices found.");
}
for (UsbDevice device : devices) {
System.out.println("\tProduct String " + device.getProductString());
System.out.println("\tManufacturer String " + device.getManufacturerString());
System.out.println("\tSerial Number " + device.getSerialNumberString());
}
What I'm doing is communication between
Desktop(Windows 8) Processing(2.2.1) Application <---- Android application(Not made by Processing, it's made on Android Studio)
I need to send data to desktop from android application (one side communication).
From android..
First, I'm getting already paired device. It means that before executing both application, I will pair desktop and android device already.
Bluetooth connection and sending outstream is on child thread.
Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
Log.i(TAG, device.getName() + "\n" + device.getAddress());
targetDevice = device;
break;
}
}else{
Log.i(TAG, "No paired device found!");
}
And then, I connect it
BluetoothSocket tmp = null;
// Get a BluetoothSocket for a connection with the
// given BluetoothDevice
try {
tmp = targetDevice.createRfcommSocketToServiceRecord(MY_UUID);
Method m = targetDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
tmp = (BluetoothSocket) m.invoke(targetDevice, 1);
} catch (Exception e) {
Log.e(TAG, "create() failed", e);
}
btSocket = tmp;
if (btSocket != null){
// Socket is created
// try to connect the socket
BluetoothConnectionThread thread = new BluetoothConnectionThread(btSocket);
thread.start();
}
From logcat(Log.i), I can see connection is successfully established. It seems that there is no problem on android side.
I'm using Serial Port Protocol UUID which is
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
And from Android side, it's completely ready to write any byte[].. and when I press dummy button on android, device sends dummy bytes to outputstream. I think it has no problem here on Android side..
Now, on desktop processing(Windows 8, processing 2.2.1 - I HAVE TO USE processing 2.0, there is no other option for some reason.), I have no any clue what I have to now.. I searched in google a lot but I could not find any clue. Now, I'm trying to use processing Serial library like
println(Serial.list());
but it gives nothing... So, I tried to initialize Serial object by
Serial port = new Serial(this, "/dev/rfcomm0", 115200);
but it gives error
Error opening serial port /dev/rfcomm0:Port busy
Is there any other way to read socket buffer(instream) which comes from Android on processing desktop?
I am sure that this is not bluetooth driver issue because I installed the recentest driver for my desktop(Qualcomm Atheros AR3012 Bluetooth 4.0 + HS)
Just for reference..desktop is actually laptop with bluetooth(ASUS UX21E) and Android device is Galaxy Tab S(267.2mm, Octa-Core 2560x1600).
I think I have not enough knowledge about bluetooth... Please lead me to some way...
I've been struggling to get the example running from below:
https://developers.google.com/eclipse/docs/getting_started
The first problem I had was didn't have installed 'Google Cloud Messaging for Android Library' in the Android SDK (obvious I know).
But now I have an issue with the auto-generated code in two files in the Android project:
GCMIntentService.java and RegisterActivity.java
The errors are:
The method getDeviceInfo(String) is undefined for the type Deviceinfoendpoint GCMIntentService.java
The method listMessages() is undefined for the type MessageEndpoint RegisterActivity.java
The method insertDeviceInfo(DeviceInfo) is undefined for the type Deviceinfoendpoint GCMIntentService.java
The method removeDeviceInfo(String) is undefined for the type Deviceinfoendpoint GCMIntentService.java
I'm using Java SDK v1.7.0_15 on Ubuntu but I also tried on Windows 7 with Java SDK v1.6 and had the same issue. Latest Android Platform 4.2.2 and Google App Engine 1.7.7. Eclipse is Juno Service Release 2.
The problem looks like they are doing some casting wrong, because there is a method getDeviceInfo for inner class DeviceInfoEndpoint inside Deviceinfoendpoint (different capatilisations).
I could try and fix it, but just wondering if I have something wrong in my setup for this to be happening?
Any help would be appreciated.
In your GCMIntentService.java class, add .deviceInfoEndpoint() after the endpoint object in the lines with errors as shown below:
DeviceInfo existingInfo = endpoint.getDeviceInfo(registration)
DeviceInfo existingInfo = endpoint.deviceInfoEndpoint().getDeviceInfo(registration)
In RegisterActivity.java change the line
messageEndpoint.listMessages().setLimit(5).execute();
to
messageEndpoint.messageEndpoint().listMessages().setLimit(5).execute();
I would make sure you are using the same version of GCM APIs as you have JARs for. There have been quite a few revisions.
I am using the following code with gcm-server.jar, listed at 19718 bytes.
The code I successfully use to send GCM messages to a device is:
public void sendMessage() {
String notificationToken = mobileDevice.getPushNotificationCode();
String deviceType = mobileDevice.getDeviceType();
Sender sender = new Sender(BROWSER_API_KEY);
Message message = new Message.Builder().addData("message", "blah blah").build();
String device = "<the key for the device you are sending to goes here>";
try {
System.out.println("Sending message...");
Result result = sender.send(message, device, 5);
System.out.println("Done sending message");
if (result.getMessageId() != null) {
System.out.println("Got message ID: " + result.getMessageId());
System.out.println("Got error code name: " + result.getErrorCodeName());
System.out.println("result: " + result);
String canonicalRegId = result.getCanonicalRegistrationId();
if (canonicalRegId != null) {
// Database has more than one record for this device.
// Replace all of this device's records with this new id
System.out.println("Got new canonical reg id: " + canonicalRegId);
}
} else {
String error = result.getErrorCodeName();
if (error.equals(com.google.android.gcm.server.Constants.ERROR_NOT_REGISTERED)) {
// application has been removed from device - unregister from database
System.out.println("Got error: " + error);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
i am trying to use my mobile phone as GSM modem.i use SMSLib for sending and receiving SMS with this modem.
the problem is that when my phone(GSM modem) receive a sms i don't notify with SMSLib.but the code overall is good for example that notifies me when GSM modem receive a call.
my code has not any bug because i only use SMSLib example code for receiving message.
the SMSLib example code is :
public class TestSinaRec
{
public void doIt() throws Exception
{
// Define a list which will hold the read messages.
List<InboundMessage> msgList;
// Create the notification callback method for inbound & status report
// messages.
InboundNotification inboundNotification = new InboundNotification();
// Create the notification callback method for inbound voice calls.
CallNotification callNotification = new CallNotification();
//Create the notification callback method for gateway statuses.
GatewayStatusNotification statusNotification = new GatewayStatusNotification();
OrphanedMessageNotification orphanedMessageNotification = new OrphanedMessageNotification();
try
{
System.out.println("Example: Read messages from a serial gsm modem.");
System.out.println(Library.getLibraryDescription());
System.out.println("Version: " + Library.getLibraryVersion());
// Create the Gateway representing the serial GSM modem.
SerialModemGateway gateway = new SerialModemGateway("modem.com4", "COM4", 115200, "Nokia", " 6303i");
// Set the modem protocol to PDU (alternative is TEXT). PDU is the default, anyway...
gateway.setProtocol(Protocols.PDU);
// Do we want the Gateway to be used for Inbound messages?
gateway.setInbound(true);
// Do we want the Gateway to be used for Outbound messages?
gateway.setOutbound(true);
// Let SMSLib know which is the SIM PIN.
gateway.setSimPin("0444");
// Set up the notification methods.
Service.getInstance().setInboundMessageNotification(inboundNotification);
Service.getInstance().setCallNotification(callNotification);
Service.getInstance().setGatewayStatusNotification(statusNotification);
Service.getInstance().setOrphanedMessageNotification(orphanedMessageNotification);
// Add the Gateway to the Service object.
Service.getInstance().addGateway(gateway);
// Similarly, you may define as many Gateway objects, representing
// various GSM modems, add them in the Service object and control all of them.
// Start! (i.e. connect to all defined Gateways)
Service.getInstance().startService();
// Printout some general information about the modem.
System.out.println();
System.out.println("Modem Information:");
System.out.println(" Manufacturer: " + gateway.getManufacturer());
System.out.println(" Model: " + gateway.getModel());
System.out.println(" Serial No: " + gateway.getSerialNo());
System.out.println(" SIM IMSI: " + gateway.getImsi());
System.out.println(" Signal Level: " + gateway.getSignalLevel() + " dBm");
System.out.println(" Battery Level: " + gateway.getBatteryLevel() + "%");
System.out.println();
// In case you work with encrypted messages, its a good time to declare your keys.
// Create a new AES Key with a known key value.
// Register it in KeyManager in order to keep it active. SMSLib will then automatically
// encrypt / decrypt all messages send to / received from this number.
//Service.getInstance().getKeyManager().registerKey("+306948494037", new AESKey(new SecretKeySpec("0011223344556677".getBytes(), "AES")));
// Read Messages. The reading is done via the Service object and
// affects all Gateway objects defined. This can also be more directed to a specific
// Gateway - look the JavaDocs for information on the Service method calls.
msgList = new ArrayList<InboundMessage>();
Service.getInstance().readMessages(msgList, MessageClasses.ALL);
for (InboundMessage msg : msgList)
System.out.println(msg);
// Sleep now. Emulate real world situation and give a chance to the notifications
// methods to be called in the event of message or voice call reception.
System.out.println("Now Sleeping - Hit <enter> to stop service.");
System.in.read();
System.in.read();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
Service.getInstance().stopService();
}
}
public class InboundNotification implements IInboundMessageNotification
{
public void process(AGateway gateway, MessageTypes msgType, InboundMessage msg)
{
if (msgType == MessageTypes.INBOUND) System.out.println(">>> New Inbound message detected from Gateway: " + gateway.getGatewayId());
else if (msgType == MessageTypes.STATUSREPORT) System.out.println(">>> New Inbound Status Report message detected from Gateway: " + gateway.getGatewayId());
System.out.println(msg);
}
}
public class CallNotification implements ICallNotification
{
public void process(AGateway gateway, String callerId)
{
System.out.println(">>> New call detected from Gateway: " + gateway.getGatewayId() + " : " + callerId);
}
}
public class GatewayStatusNotification implements IGatewayStatusNotification
{
public void process(AGateway gateway, GatewayStatuses oldStatus, GatewayStatuses newStatus)
{
System.out.println(">>> Gateway Status change for " + gateway.getGatewayId() + ", OLD: " + oldStatus + " -> NEW: " + newStatus);
}
}
public class OrphanedMessageNotification implements IOrphanedMessageNotification
{
public boolean process(AGateway gateway, InboundMessage msg)
{
System.out.println(">>> Orphaned message part detected from " + gateway.getGatewayId());
System.out.println(msg);
// Since we are just testing, return FALSE and keep the orphaned message part.
return false;
}
}
public static void main(String args[])
{
TestSinaRec app = new TestSinaRec();
try
{
app.doIt();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
program output is for example :
Gateway Status change for modem.com4, OLD: STOPPED -> NEW: STARTING
Gateway Status change for modem.com4, OLD: STARTING -> NEW: STARTED
Modem Information: Manufacturer: Nokia Model: Nokia 6303i classic
Serial No: 355382041051833 SIM IMSI: ** MASKED ** Signal Level:
-57 dBm Battery Level: 91%
Now Sleeping - Hit to stop service.
New call detected from Gateway: modem.com4 : +989111007483
New call detected from Gateway: modem.com4 : +989111007483
when i searched for this issue i found this :
The correct operation of this method depends on the unsolicited modem
indications and on the correct operation of the CNMI command. If you
see that you are failing to receive messages using a callback method,
probably the modem indications have not been setup correctly.
so i changed my phone(my GSM modem) with Nokia 6303i rather than Nokia 5200 that i used first but the problem didn't solve.
so now i really don't know the problem will solve with choosing another phones ?! or i should search for a better and more reasonable solution.
thank you for any bit of help for solving this problem.
Well the only thing I can think of is that you're starting the Service and then sending the SMS to the modem. Because of this, this line won't be called: Service.getInstance().readMessages(msgList, MessageClasses.ALL);. However, you should still get the notification that a new message has arrived at the modem.
Try implementing the InboundNotification to fetch the messages when it senses any new messages on the modem. Do this by overriding the process() method.
However, it might also be due to the fact that you're actually pressing <Enter> too soon. As the comment say; you have to wait go give the notifications method a chance to be called.
Sometimes it's just something as silly as that. Let me know if any of it helped or if I completely misunderstood your problem. I'm working on a multi-modem gateway myself, so I'd be happy to help.
i had an issue with a GT-I9000 he received the inbound alert but couldn't fetch it the right sms object, i think this is a matter of the Storage Location,
i tried with another phone (Samsung GT-S5670 Android) of a friend of mine who had some messages stored on the SIM Card Memory, the smslibrary was notified and the ReadMessages Class logged all the messages.
so i think you need to find somehow to change storage location on the ReadMessages.java or find an compatible phone that can stores the sms to Sim Card instead of the phone memory.
hope this help.
The problem was with my phone.Smslib doesn't work in listening sms for a variety of phones(including smartphones,most of Nokia phones,etc.).I didn't check but probably this problem will be solved if you use a dedicated GSM modem(like huawei GSM modems)