Android Question SEND TEXT --> DISPLAY BLUETOOTH

MarcoRome

Expert
Licensed User
Longtime User
Hi all.
How can i send a string to a bluetooth device (device type of the car, advertising billboards, etc) that simply displays the string sent ??

Example i have this string: "Hi today is xx/yy/zzzz, you have discount XX%".
This message should be displayed on the device.
Thank you
Marco
 

Robert Valentino

Well-Known Member
Licensed User
Longtime User
upload_2016-8-28_10-38-0.png


Changed to android-23

and now kind of getting the same thing:

upload_2016-8-28_10-38-59.png
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I only got an Email about the Beta of B4A 6 being release and I normally do not install betas.

Is there an actual release?

BobVal

Updated to the V6 Beta and still same problem
Tried changing to Android-24 (android.jar) still same problem
Tried changing v4 to v7 still same problem

But the files do exist in
upload_2016-8-29_15-36-23.png


AS Always I am so confused.
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
I only got an Email about the Beta of B4A 6 being release and I normally do not install betas.

Is there an actual release?

BobVal

Updated to the V6 Beta and still same problem
Tried changing to Android-24 (android.jar) still same problem
Tried changing v4 to v7 still same problem

But the files do exist in
View attachment 47366

AS Always I am so confused.
Strain because i havent this error and work without problem. Can you share your project ?
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
This code compiles correctly:

I am still trying to send the Track, Title, ... to my cars display


I tried to take Erels code above and modify it to user MediaSessionCompat as the last entry shown in this example: http://stackoverflow.com/questions/15527614/send-track-informations-via-a2dp-avrcp


B4X:
You don't need to control SDK_INT if you are using Compat version of components. Below code tested with many car bluetooth devices and works like charm. Some devices don't understand some KEYs so it's better to use possible KEY. Reference. Don't forget to .build() after putBitmap not before

#if JAVA
import android.media.AudioManager;
import android.media.AudioTrack;
import android.media.MediaCodec;
import android.media.MediaCodec.BufferInfo;
import android.media.MediaExtractor;
import android.media.MediaFormat;
import android.media.MediaMetadata;
import android.media.RemoteControlClient;
import android.media.RemoteControlClient.MetadataEditor;
import android.media.session.MediaSession;
import android.media.session.PlaybackState;
import android.os.Binder;
import android.os.Build.VERSION;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.SystemClock;
import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaSessionCompat;                             //  I added this line
import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
import android.support.v7.media.MediaItemMetadata;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.text.format.Time;
import android.util.Log;
import android.content.Intent;
import android.content.IntentFilter;
import android.app.PendingIntent;
import android.app.Service;
import com.google.android.gms.cast.TextTrackStyle;

private AudioManager mAudioManager;
private AudioTrack mAudioTrack;
private RemoteControlClient mRemoteControlClient;
private MediaSession mMediaSession;
private MediaSessionCompat mMediaSessionCmp;                                              //  I added this line

public void _onCreate() {
  BA.Log("Running onCreate");
    IntentFilter filter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
       this.mAudioManager = (AudioManager) getSystemService("audio");
     
  if (VERSION.SDK_INT < 21) {
     BA.Log("Version Less then 21");
     if (this.mRemoteControlClient == null) {
       Log.d("init()", "API " + VERSION.SDK_INT + " lower than " + 21);
       Log.d("init()", "Using RemoteControlClient API.");
   
//-----------------------------
//  Tried with or with the lines below still crashes
//       this.mRemoteControlClient = new RemoteControlClient(PendingIntent.getBroadcast(this, 0, new Intent("android.intent.action.MEDIA_BUTTON"), 0));
//       this.mAudioManager.registerRemoteControlClient(this.mRemoteControlClient);
     }
     } else if (this.mMediaSession == null) {
           BA.Log("Version greater then or equal 21");

           Log.d("init()", "API " + VERSION.SDK_INT + " greater or equals " + 21);
           Log.d("init()", "Using MediaSession API.");
           this.mMediaSession = new MediaSession(this, "StreamPlayerServiceMediaSession");
           this.mMediaSession.setFlags(2);
           this.mMediaSession.setActive(true);
       }
   
//-----------------------------------------------------------
//  My new code
//-----------------------------------------------------------
   if  (this.mMediaSessionCmp == null) {
     BA.Log("Creating MediaSessionCmp");
     this.mMediaSessionCmp = new MediaSessionCompat(this, "PlayerServiceMediaSession");      <===== My Code crashes here
     this.mMediaSessionCmp.setFlags(2);
     this.mMediaSessionCmp.setActive(true);
     BA.Log("Created");   
   }
}
   
public void sendTrackInfoToBluetoothDevice() {
   BA.Log("sendTrackInfo");
   
  if (VERSION.SDK_INT >= 21) {
  BA.Log("API " + VERSION.SDK_INT + " greater than " + 21);
 
  this.mMediaSession.setMetadata(new MediaMetadata.Builder().putString(MediaItemMetadata.KEY_TITLE, "Erel(A)-title").putString(MediaItemMetadata.KEY_ARTIST, "Erel(A)-artist").putString(MediaMetadataCompat.METADATA_KEY_ALBUM, "Erel(A)-album").build());
  this.mMediaSession.setPlaybackState(new PlaybackState.Builder().setActions(4).setState(3, -1, TextTrackStyle.DEFAULT_FONT_SCALE, SystemClock.elapsedRealtime()).build());
  } else if (VERSION.SDK_INT >= 18) {
  BA.Log("API " + VERSION.SDK_INT + " greater than " + 18);

if (mAudioManager.isBluetoothA2dpOn() && mMediaSessionCmp != null) {
   BA.Log("Creating MediaMetadataCompat");
 
  String songTitle = "MMC-Title";
  String artistTitle = "MMC-Artist";
  long duration = 123;

  final MediaMetadataCompat.Builder metadata = new MediaMetadataCompat.Builder();

  metadata.putString(MediaMetadataCompat.METADATA_KEY_TITLE, songTitle);
  metadata.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, songTitle);
  metadata.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, artistTitle);
  metadata.putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, artistTitle);
  metadata.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, duration);  

   BA.Log("Sending MediaMetadataCompat");
     
     this.mMediaSessionCmp.setMetadata(metadata.build());     
 
   BA.Log("Send MediaMetadataCompat");
}
  }
}
#end if

My code crashes when I try and create the mMediaSessionCmp
B4X:
Installing file.
Receiver - Create
Running onCreate
Version Less then 21
Creating MediaSessionCmp
java.lang.RuntimeException: Unable to create service b4a.example.receiver: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
   at android.app.ActivityThread.handleCreateService(ActivityThread.java:2816)
   at android.app.ActivityThread.access$1900(ActivityThread.java:172)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:136)
   at android.app.ActivityThread.main(ActivityThread.java:5591)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
   at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
   at anywheresoftware.b4a.BA.runHook(BA.java:145)
   at b4a.example.receiver.onCreate(receiver.java:87)
   at android.app.ActivityThread.handleCreateService(ActivityThread.java:2806)
   ... 10 more
Caused by: java.lang.reflect.InvocationTargetException
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at anywheresoftware.b4a.BA.runHook(BA.java:142)
   ... 12 more
Caused by: java.lang.IllegalArgumentException: MediaButtonReceiver component may not be null.
   at android.support.v4.media.session.MediaSessionCompat$MediaSessionImplBase.<init>(MediaSessionCompat.java:1347)
   at android.support.v4.media.session.MediaSessionCompat.<init>(MediaSessionCompat.java:228)
   at android.support.v4.media.session.MediaSessionCompat.<init>(MediaSessionCompat.java:190)
   at b4a.example.receiver._onCreate(receiver.java:553)   ... 15 more

Do not understand what it means when saying MediaButtonReceiver component may not be null
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Figured out that I needed to add

AddReceiverText(Receiver,
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>)

to my manifest


Now works - Well sort of. Still nothing being displayed
 
Upvote 0
Top