简体   繁体   中英

Android Bluetooth Chat App & Sending Spotify Song Names

I'm am fairly new to Android, and I'm trying to develop an app for a small project of mine using an example Bluetooth Chat App and Spotify Media Notifications. I could not provide the links because stack overflow did not let me but they are available online with a quick google search. I was wondering why I was getting this exception and is there any solution to this?

java.lang.NullPointerException

The line numbers correspond to each of the pieces of code below:

Line 269 if (message.length() > 0)

Line 56 public class BluetoothChatFragment extends Fragment

Line 131 sendMessage(artistName);

How my code is set up:

I have a static broadcast receiver set up exactly like the Spotify link below. I grab the song name and artist and package them into strings. Then I have another broadcast receiver in my Bluetooth fragment. I used another one because I did not know how to get the string into the Bluetooth fragment because I needed to call sendMessage() a private method in the fragment class. It seems like the exception is throwing when I call sendMessage(string) in my fragment.

Static Spotify Broadcast Receiver

package com.example.android.bluetoothchat;

import android.content.BroadcastReceiver;
import android.content.Context; 
import android.content.Intent;

import com.example.android.common.logger.Log;

public class MyBroadcastReceiver extends BroadcastReceiver {
static final class BroadcastTypes {
    static final String SPOTIFY_PACKAGE = "com.spotify.music";
    static final String PLAYBACK_STATE_CHANGED = SPOTIFY_PACKAGE + ".playbackstatechanged";
    static final String QUEUE_CHANGED = SPOTIFY_PACKAGE + ".queuechanged";
    static final String METADATA_CHANGED = SPOTIFY_PACKAGE + ".metadatachanged";
}

@Override
public void onReceive(Context context, Intent intent) {
    // This is sent with all broadcasts, regardless of type. The value is taken from
    // System.currentTimeMillis(), which you can compare to in order to determine how
    // old the event is.
    long timeSentInMs = intent.getLongExtra("timeSent", 0L);

    Intent sendBack = new Intent("Received");

    String action = intent.getAction();

    String artistName;
    String trackName;


    if (action.equals(BroadcastTypes.METADATA_CHANGED)) {
        artistName = intent.getStringExtra("artist");
        trackName = intent.getStringExtra("track");
        String msg = "S a " + artistName + '\0';
        String msg2 = "S n " + trackName + '\0';
        Log.d("Song", msg);
        Log.d("Artist", msg2);

        sendBack.putExtra("Song", msg);
        sendBack.putExtra("Artist", msg2);



    } else if (action.equals(BroadcastTypes.PLAYBACK_STATE_CHANGED)) {
        boolean playing = intent.getBooleanExtra("playing", false);
        // Do something with extracted information
        if(playing) {
            Log.d("Playing", "S p\0");
            sendBack.putExtra("Playing", "S p\0");
        }
        else {
            Log.d("Playing", "S s\0");
            sendBack.putExtra("Playing", "S s\0");
        }

    }
    context.sendBroadcast(sendBack);

    }
}

Broadcast Receiver in Fragment:

    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        String artistName = intent.getStringExtra("Song");
        String trackName = intent.getStringExtra("Artist");
        String playing = intent.getStringExtra("Playing");

        Log.d("Please", artistName);
        Log.d("Please", trackName);
        Log.d("Please", playing);

        sendMessage(artistName);
        sendMessage(trackName);
        sendMessage(playing);

        }
    };

sendMessage Method():

 private void sendMessage(String message) {
    // Check that we're actually connected before trying anything
    if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) {
        Toast.makeText(getActivity(), R.string.not_connected,      Toast.LENGTH_SHORT).show();
        return;
    }

    // Check that there's actually something to send
    if (message.length() > 0) {
        // Get the message bytes and tell the BluetoothChatService to write
        byte[] send = message.getBytes();
        mChatService.write(send);

        // Reset out string buffer to zero and clear the edit text field
        mOutStringBuffer.setLength(0);
        mOutEditText.setText(mOutStringBuffer);
    }
}

Any help would be much appreciated!

http://developer.android.com/samples/BluetoothChat/index.html

To me it looks like sendMessage gets called with null instead of an actual string. Note that intent.getStringExtra(some_string) can return null, if some_string cannot be found.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM