Hi guys! I am starting this thread for this Bluetooth audio fix patch from Lineage OS JIRA website, to be visible for most developers that haven't seen it. I don't know if the patch is already included to latest nightlies and i don't know if it works. If someone can tell us how to use it or make flashable zip to make our LAOS roms bt audio work properly
Patch: https://jira.lineageos.org/secure/attachment/11785/11785_bluetooth_fix.patch
I couldn't make it work
I had patched the audio_policy.conf file in /sytem/etc manually in last version but it didn't work.
Then I tried applying the patch manually and then using git but it doesn't work neither in last version nor the previous from June 19.
The resulting file was exactly equal in both versions:
Code:
# Global configuration section: lists input and output devices always present on the device
# as well as the output device selected by default.
# Devices are designated by a string that corresponds to the enum in audio.h
global_configuration {
attached_output_devices AUDIO_DEVICE_OUT_EARPIECE|AUDIO_DEVICE_OUT_SPEAKER|AUDIO_DEVICE_OUT_TELEPHONY_TX
default_output_device AUDIO_DEVICE_OUT_SPEAKER
attached_input_devices AUDIO_DEVICE_IN_BUILTIN_MIC|AUDIO_DEVICE_IN_BACK_MIC|AUDIO_DEVICE_IN_REMOTE_SUBMIX|AUDIO_DEVICE_IN_VOICE_CALL|AUDIO_DEVICE_IN_TELEPHONY_RX|AUDIO_DEVICE_IN_FM_TUNER
}
# audio hardware module section: contains descriptors for all audio hw modules present on the
# device. Each hw module node is named after the corresponding hw module library base name.
# For instance, "primary" corresponds to audio.primary.<device>.so.
# The "primary" module is mandatory and must include at least one output with
# AUDIO_OUTPUT_FLAG_PRIMARY flag.
# Each module descriptor contains one or more output profile descriptors and zero or more
# input profile descriptors. Each profile lists all the parameters supported by a given output
# or input stream category.
# The "channel_masks", "formats", "devices" and "flags" are specified using strings corresponding
# to enums in audio.h and audio_policy.h. They are concatenated by use of "|" without space or "\n".
audio_hw_modules {
primary {
outputs {
primary {
sampling_rates 44100|48000
channel_masks AUDIO_CHANNEL_OUT_STEREO
formats AUDIO_FORMAT_PCM_16_BIT
devices AUDIO_DEVICE_OUT_EARPIECE|AUDIO_DEVICE_OUT_SPEAKER|AUDIO_DEVICE_OUT_WIRED_HEADSET|AUDIO_DEVICE_OUT_WIRED_HEADPHONE|AUDIO_DEVICE_OUT_BLUETOOTH_SCO|AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET|AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT|AUDIO_DEVICE_OUT_ALL_SCO|AUDIO_DEVICE_OUT_AUX_DIGITAL|AUDIO_DEVICE_OUT_PROXY
flags AUDIO_OUTPUT_FLAG_PRIMARY
}
low_latency {
sampling_rates 44100|48000
channel_masks AUDIO_CHANNEL_OUT_STEREO
formats AUDIO_FORMAT_PCM_16_BIT
devices AUDIO_DEVICE_OUT_SPEAKER|AUDIO_DEVICE_OUT_EARPIECE|AUDIO_DEVICE_OUT_WIRED_HEADSET|AUDIO_DEVICE_OUT_WIRED_HEADPHONE|AUDIO_DEVICE_OUT_BLUETOOTH_SCO|AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET|AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT|AUDIO_DEVICE_OUT_ALL_SCO|AUDIO_DEVICE_OUT_AUX_DIGITAL|AUDIO_DEVICE_OUT_PROXY
flags AUDIO_OUTPUT_FLAG_FAST
}
multichannel {
sampling_rates 44100|48000
channel_masks dynamic
formats AUDIO_FORMAT_PCM_16_BIT
devices AUDIO_DEVICE_OUT_AUX_DIGITAL|AUDIO_DEVICE_OUT_PROXY
flags AUDIO_OUTPUT_FLAG_DIRECT
}
compress_offload {
sampling_rates 8000|11025|16000|22050|32000|44100|48000
channel_masks AUDIO_CHANNEL_OUT_MONO|AUDIO_CHANNEL_OUT_STEREO|AUDIO_CHANNEL_OUT_2POINT1|AUDIO_CHANNEL_OUT_QUAD|AUDIO_CHANNEL_OUT_PENTA|AUDIO_CHANNEL_OUT_5POINT1|AUDIO_CHANNEL_OUT_6POINT1|AUDIO_CHANNEL_OUT_7POINT1
formats AUDIO_FORMAT_MP3|AUDIO_FORMAT_AAC_LC|AUDIO_FORMAT_AAC_HE_V1|AUDIO_FORMAT_AAC_HE_V2|AUDIO_FORMAT_AC3|AUDIO_FORMAT_E_AC3
devices AUDIO_DEVICE_OUT_SPEAKER|AUDIO_DEVICE_OUT_EARPIECE|AUDIO_DEVICE_OUT_WIRED_HEADSET|AUDIO_DEVICE_OUT_WIRED_HEADPHONE|AUDIO_DEVICE_OUT_BLUETOOTH_SCO|AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET|AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT|AUDIO_DEVICE_OUT_ALL_SCO|AUDIO_DEVICE_OUT_AUX_DIGITAL|AUDIO_DEVICE_OUT_PROXY
flags AUDIO_OUTPUT_FLAG_DIRECT|AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD|AUDIO_OUTPUT_FLAG_NON_BLOCKING
}
incall_music {
sampling_rates 8000|16000|48000
channel_masks AUDIO_CHANNEL_OUT_MONO
formats AUDIO_FORMAT_PCM_16_BIT
devices AUDIO_DEVICE_IN_BUILTIN_MIC|AUDIO_DEVICE_IN_WIRED_HEADSET|AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET|AUDIO_DEVICE_IN_BACK_MIC|AUDIO_DEVICE_IN_FM_TUNER|AUDIO_DEVICE_IN_VOICE_CALL
flags AUDIO_OUTPUT_FLAG_DIRECT|AUDIO_OUTPUT_FLAG_INCALL_MUSIC
}
voice_tx {
sampling_rates 8000|16000|48000
channel_masks AUDIO_CHANNEL_OUT_STEREO|AUDIO_CHANNEL_OUT_MONO
formats AUDIO_FORMAT_PCM_16_BIT
devices AUDIO_DEVICE_OUT_TELEPHONY_TX
}
voip_rx {
sampling_rates 8000|16000
channel_masks AUDIO_CHANNEL_OUT_MONO
formats AUDIO_FORMAT_PCM_16_BIT|AUDIO_FORMAT_AMR_NB|AUDIO_FORMAT_AMR_WB|AUDIO_FORMAT_QCELP|AUDIO_FORMAT_EVRC|AUDIO_FORMAT_EVRCB|AUDIO_FORMAT_EVRCWB|AUDIO_FORMAT_EVRCNW
devices AUDIO_DEVICE_OUT_SPEAKER|AUDIO_DEVICE_OUT_EARPIECE|AUDIO_DEVICE_OUT_WIRED_HEADSET|AUDIO_DEVICE_OUT_WIRED_HEADPHONE|AUDIO_DEVICE_OUT_BLUETOOTH_SCO|AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET|AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT|AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET|AUDIO_DEVICE_OUT_ALL_SCO
flags AUDIO_OUTPUT_FLAG_DIRECT|AUDIO_OUTPUT_FLAG_VOIP_RX
}
}
inputs {
primary {
sampling_rates 8000|11025|12000|16000|22050|24000|32000|44100|48000
channel_masks AUDIO_CHANNEL_IN_5POINT1|AUDIO_CHANNEL_IN_MONO|AUDIO_CHANNEL_IN_STEREO|AUDIO_CHANNEL_IN_FRONT_BACK
formats AUDIO_FORMAT_PCM_16_BIT|AUDIO_FORMAT_AMR_NB|AUDIO_FORMAT_AMR_WB|AUDIO_FORMAT_QCELP|AUDIO_FORMAT_EVRC|AUDIO_FORMAT_EVRCB|AUDIO_FORMAT_EVRCWB|AUDIO_FORMAT_EVRCNW
devices AUDIO_DEVICE_IN_BUILTIN_MIC|AUDIO_DEVICE_IN_WIRED_HEADSET|AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET|AUDIO_DEVICE_IN_BACK_MIC|AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET|AUDIO_DEVICE_IN_FM_TUNER|AUDIO_DEVICE_IN_VOICE_CALL
}
voice_rx {
sampling_rates 8000|16000|48000
channel_masks AUDIO_CHANNEL_IN_STEREO|AUDIO_CHANNEL_IN_MONO
formats AUDIO_FORMAT_PCM_16_BIT
devices AUDIO_DEVICE_IN_TELEPHONY_RX
}
}
}
a2dp {
outputs {
a2dp {
sampling_rates 44100
channel_masks AUDIO_CHANNEL_OUT_STEREO
formats AUDIO_FORMAT_PCM_16_BIT
devices AUDIO_DEVICE_OUT_ALL_A2DP
}
}
}
usb {
outputs {
usb_accessory {
sampling_rates 44100
channel_masks AUDIO_CHANNEL_OUT_STEREO
formats AUDIO_FORMAT_PCM_16_BIT
devices AUDIO_DEVICE_OUT_USB_ACCESSORY
}
usb_device {
sampling_rates dynamic
channel_masks dynamic
formats dynamic
devices AUDIO_DEVICE_OUT_USB_DEVICE
}
}
inputs {
usb_device {
sampling_rates dynamic
channel_masks AUDIO_CHANNEL_IN_STEREO
formats AUDIO_FORMAT_PCM_16_BIT
devices AUDIO_DEVICE_IN_USB_DEVICE
}
}
}
r_submix {
outputs {
submix {
sampling_rates 48000
channel_masks AUDIO_CHANNEL_OUT_STEREO
formats AUDIO_FORMAT_PCM_16_BIT
devices AUDIO_DEVICE_OUT_REMOTE_SUBMIX
}
}
inputs {
submix {
sampling_rates 48000
channel_masks AUDIO_CHANNEL_IN_STEREO
formats AUDIO_FORMAT_PCM_16_BIT
devices AUDIO_DEVICE_IN_REMOTE_SUBMIX
}
}
}
}
Related
With the enhancement of D. Yeager for CM9, we can enable the Host Card Emulation functionality without any Secure Element.
by now, it can only change the SAK, ATQA, APP_DATA,... by modifying the CM9 source code. but when changing the uid, we'll get the lower-layer error: by logcating, it shows 'ANY_E_REG_ACCESS_DENIED' according to ETSI TS 102 622.
so, this operation must be blocked by the CLF's software stack, i guess - for i have no PN544/PN65N's manual.
but, i want to ask, is there any possible to change the rid to uid of host card emulation for nuxus s?
Reference:
Modifyed Code:
loc: android_external_libnfc-nxp.rar\src\phHciNfc_CE_A.c
Code:
NFCSTATUS
phHciNfc_CE_A_Initialise(
phHciNfc_sContext_t *psHciContext,
void *pHwRef
)
{
/*
1. Open Pipe,
2. Set all parameters
*/
NFCSTATUS status = NFCSTATUS_SUCCESS;
static uint8_t sak = 0x38; // HOST_CE_A_SAK_DEFAULT;
static uint8_t uid_info[] = { 0xE0U, 0x15U, 0x07U, 0x06U };
static uint8_t atqa_info[] = { NXP_CE_A_ATQA_LOW,
NXP_CE_A_ATQA_HIGH
};
if ((NULL == psHciContext) || (NULL == pHwRef))
{
status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
}
else if(NULL == psHciContext->p_ce_a_info)
{
status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
}
else
{
phHciNfc_CE_A_Info_t *ps_ce_a_info = ((phHciNfc_CE_A_Info_t *)
psHciContext->p_ce_a_info );
phHciNfc_Pipe_Info_t *ps_pipe_info = NULL;
ps_pipe_info = ps_ce_a_info->p_pipe_info;
if(NULL == ps_pipe_info )
{
status = PHNFCSTVAL(CID_NFC_HCI,
NFCSTATUS_INVALID_HCI_INFORMATION);
}
else
{
switch(ps_ce_a_info->current_seq)
{
case HOST_CE_A_PIPE_OPEN:
{
status = phHciNfc_Open_Pipe( psHciContext,
pHwRef, ps_pipe_info );
if(status == NFCSTATUS_SUCCESS)
{
//{----------------------------------- new for uid
ps_ce_a_info->next_seq = HOST_CE_A_UID_SEQ; // HOST_CE_A_SAK_SEQ;
//}----------------------------------- new for uid
status = NFCSTATUS_PENDING;
}
break;
}
//{----------------------------------- new for uid
case HOST_CE_A_UID_SEQ:
{
/* HOST Card Emulation A UID Configuration */
ps_pipe_info->reg_index = HOST_CE_A_UID_REG_INDEX;
/* Configure the UID of Host Card Emulation A */
ps_pipe_info->param_info = (void*)uid_info ;
ps_pipe_info->param_length = sizeof(uid_info) ;
status = phHciNfc_Send_Generic_Cmd(psHciContext,pHwRef,
ps_pipe_info->pipe.pipe_id,
(uint8_t)ANY_SET_PARAMETER);
if(status == NFCSTATUS_PENDING)
{
ps_ce_a_info->next_seq = HOST_CE_A_SAK_SEQ;
}
break;
}
//}----------------------------------- new for uid
case HOST_CE_A_SAK_SEQ:
{
/* HOST Card Emulation A SAK Configuration */
ps_pipe_info->reg_index = HOST_CE_A_SAK_INDEX;
/* Configure the SAK of Host Card Emulation A */
sak = (uint8_t)HOST_CE_A_SAK_DEFAULT;
ps_pipe_info->param_info =(void*)&sak ;
ps_pipe_info->param_length = sizeof(sak) ;
status = phHciNfc_Send_Generic_Cmd(psHciContext,pHwRef,
ps_pipe_info->pipe.pipe_id,
(uint8_t)ANY_SET_PARAMETER);
if(status == NFCSTATUS_PENDING)
{
ps_ce_a_info->next_seq = HOST_CE_A_ATQA_SEQ;
}
break;
}
case HOST_CE_A_ATQA_SEQ:
{
/* HOST Card Emulation A ATQA Configuration */
ps_pipe_info->reg_index = HOST_CE_A_ATQA_INDEX;
/* Configure the ATQA of Host Card Emulation A */
ps_pipe_info->param_info = (void*)atqa_info ;
ps_pipe_info->param_length = sizeof(atqa_info) ;
status = phHciNfc_Send_Generic_Cmd(psHciContext,pHwRef,
ps_pipe_info->pipe.pipe_id,
(uint8_t)ANY_SET_PARAMETER);
if(status == NFCSTATUS_PENDING)
{
ps_ce_a_info->next_seq = HOST_CE_A_ENABLE_SEQ;
}
break;
}
case HOST_CE_A_ENABLE_SEQ:
{
status = phHciNfc_CE_A_Mode( psHciContext,
pHwRef, HOST_CE_MODE_ENABLE );
if(status == NFCSTATUS_PENDING)
{
ps_ce_a_info->next_seq = HOST_CE_A_DISABLE_SEQ;
status = NFCSTATUS_SUCCESS;
}
break;
}
default :
{
break;
}
}
}
}
return status;
}
> sorry, i'm new to the xda, and cannot post externanl links with this thread.
if the reason is coming from the clf's software stack - the fireware, so, can we change it by disassamble it to unlock this block?
maybe there is an answer to this, thank u ^)^
Hey.
I am not entirely sure how to log data from a function.
I am trying to log the accessed URL from the first of the 3 functions in the code pasted below.
I have found the method, but I don't know if It is possible to log the output
Code:
Method - name = HttpConnector.java:
package dk.inlogic.app.res;
private static String getAction(Context context, String action, String version) {
try {
String result = getURLaction(context, action, PreferenceConnector.readString(context, PreferenceConnector.USERNAME, ""), PreferenceConnector.readString(context, PreferenceConnector.PASSWORD, ""), version);
if (result != null) {
return result;
}
} catch (Exception e) {
Log.e("HttpConnector getAction Exception", "action = " + action + ", version = " + version + ", " + e.toString());
}
return "";
}
public static String getTimetable(Context context) {
return getAction(context, "timetable", "2");
}
File 2 - Name, TimetableFragment.java:
package dk.inlogic.app.timetable;
protected String doInBackground(String... args) {
try {
Log.i("TimetableFragment ProgressTask doInBackground", "check for new file");
if (AppInfo.instance().isLive() && HttpConnector.getUpdateNowIfOld("rss2.xml")) {
Log.i("TimetableFragment ProgressTask doInBackground", "Get new file");
saveFile(HttpConnector.getTimetable(this.context));
return "";
} else {
if (!AppInfo.instance().isLive()) {
new DemoDownloadThread(TimetableFragment.this.getActivity()).start();
}
return "";
}
} catch (Exception e) {
Log.e("TimetableFragment doInBackground Exception", e.toString());
}
}
If by "output" you mean result, then you can use param.getResult.
If that's not it, I don't understand what you're trying to do.
I'm making an Android Studio app to control my Arduino project via Bluetooth. I've successfully connected the HC-06 module to the app with the following class:
class ConnectBT extends AsyncTask<Void, Void, Void>{
private boolean ConnectSuccess = true;
@override
protected void onPreExecute() {
}
@override
protected Void doInBackground(Void... devices)
{
try
{
if (Drive.btSocket == null || !Drive.isBtConnected)
{
Drive.myBluetooth = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice dispositivo = Drive.myBluetooth.getRemoteDevice(Drive.BTaddress);
Drive.btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(Drive.myUUID);
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
Drive.btSocket.connect();
}
}
catch (IOException e)
{
ConnectSuccess = false;exception here
}
return null;
}
@override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
if (!ConnectSuccess)
{
msg("Connection Failed. Is it a SPP Bluetooth? Try again.");
}
else
{
msg("Connected.");
Drive.loadingScreen.setVisibility(View.GONE);
System.out.println("Connected");
Drive.isBtConnected = true;
}
//Drive.progress.dismiss();
}
private void msg(String s)
{
//Toast.makeText(Drive.class.,s,Toast.LENGTH_LONG).show();
}
}
But now when I tried to send and receive data from the app things got hard, so I'm stuck. I've googled on how to send and receive data but I mostly find tutorials on how to make a BT app from scratch. How do I send and receive data from HC-06 via Bluetooth in an Android app? Any code snippets? Sorry for my bad explanation, I'm a self-learned beginner. Br Imim_B
Solved it!
Solved it with the following snippet:
void sendData(String data){
if (isBtConnected){
try {
Drive.btSocket.getOutputStream().write((data).getBytes());
} catch (IOException e) {
e.printStackTrace();
msg("Failed to send Bluetooth data");
}
}
}
Hello everyone! I have just made a program in Android Studio which should be a Quiz. If you could help me to find the mistake in my code I would be very grateful. Probably it's just a little mistake (as I am beginner...).
At the beginning I have created an arraylist, that does contain the strings a,b,c,d. Then, the program should make a few questions, that can be answered with Yes or No. If the user clicks the button yes, some strings are going to be removed from the Arraylist. This should happen until only one string is in the arraylist.
Here is my code:
package com.example.test;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.Arrays;
import java.util.Collections;
import java.util.*;
public class MainActivity extends AppCompatActivity {
static ArrayList<String> p = new ArrayList<String>();
static Button endbutton;
static Button questionbutton;
static Button yesbutton;
static Button nobutton;
static boolean yesbuttonisclicked=false;
static boolean nobuttonisclicked=false;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
endbutton = findViewById(R.id.endbutton);
questionbutton = findViewById(R.id.questionbutton);
yesbutton = findViewById(R.id.yesbutton);
nobutton = findViewById(R.id.nobutton);
p.add("a");
p.add("b");
p.add("c");
p.add("d");
yesbutton.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View v) {
yesbuttonisclicked=true;
nobuttonisclicked=false;
}
});
nobutton.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View v) {
nobuttonisclicked=true;
yesbuttonisclicked=false;
}
});
List array1 = Arrays.asList("1", "2", "3", "4");
Collections.shuffle(array1);
int i=0;
if (array1.get(i) == "1") {
questionbutton.setText("Question1");
if (yesbuttonisclicked) {
p.remove("a");
p.remove("b");
if (p.size() == 1) {
endbutton.setText(p.get(0));
}
} else {
p.remove("c");
p.remove("d");
if (p.size() == 1) {
endbutton.setText(p.get(0));
}
}
} else if (array1.get(i).equals("2")) {
questionbutton.setText("Question2");
if (yesbuttonisclicked) {
p.remove("a");
p.remove("c");
if (p.size() == 1) {
endbutton.setText(p.get(0));
}
} else {
p.remove("b");
p.remove("d");
if (p.size() == 1) {
endbutton.setText(p.get(0));
}
}
} else if (array1.get(i).equals("3")) {
questionbutton.setText("Question3");
if (yesbuttonisclicked) {
p.remove("a");
p.remove("d");
if (p.size() == 1) {
endbutton.setText(p.get(0));
}
} else {
p.remove("c");
p.remove("b");
if (p.size() == 1) {
endbutton.setText(p.get(0));
}
}
} else if (array1.get(i).equals("4")) {
questionbutton.setText("Question4");
if (yesbuttonisclicked) {
p.remove("d");
p.remove("c");
if (p.size() == 1) {
endbutton.setText(p.get(0));
}
} else {
p.remove("a");
p.remove("b");
if (p.size() == 1) {
endbutton.setText(p.get(0));
}
}
}
}
}
Hello, I am learning to program in android and I have the following project. I need to download two XML files from the internet, these files correspond to fuel prices and the location of gas stations. The location and price come in different files, to join these files I need to identify the id of the service station and create a new file with the location and price of the fuel of each station. I am learning how to perform each of these steps. In the code that I add below I want to download a file, save it in memory and display it in TextView. The code does not work correctly, I manage to download the file but I cannot save it in memory to later show it in the Textview. The code downloads the data in the / data / folder, in order to verify that the connection is made and the data is downloaded. How can I download a file and save it in memory to later parse the XML?
Button button=(Button) findViewById(R.id.DownloadBt);
txt = (TextView) findViewById(R.id.txt);
button.setOnClickListener(this);
}
@override
public void onClick(View view){
switch (view.getId()){
case R.id.DownloadBt:
descargarPDF();
break;
}
}
void descargarPDF(){
String urlDescargarPlaces = "https://publicacionexterna.azurewebsites.net/publicaciones/places";
//String urlDescargarPrices = "https://publicacionexterna.azurewebsites.net/publicaciones/prices";
DescargarPDFAsyncTask descargarPlaces=new DescargarPDFAsyncTask("places.xml");
//DescargarPDFAsyncTask descargarPrices=new DescargarPDFAsyncTask("prices.xml");
descargarPlaces.execute(urlDescargarPlaces);
//descargarPrices.execute(urlDescargarPrices);
}
class DescargarPDFAsyncTask extends AsyncTask<String, Integer, String> {
String mFileName;
XMLParse xmlParse=new XMLParse();
DescargarPDFAsyncTask(String fileName){
mFileName=fileName;
}
@override
protected void onPreExecute(){
super.onPreExecute();
}
@override
protected String doInBackground(String... urlFichero) {
String urlADescargar=urlFichero[0];
HttpURLConnection conexion=null;
InputStream input=null;
OutputStream output=null;
try {
URL url = new URL(urlADescargar);
conexion = (HttpURLConnection) url.openConnection();
conexion.connect();
if(conexion.getResponseCode() != HttpURLConnection.HTTP_OK) {
return "Connection not successful";
}
input=conexion.getInputStream();
String rutaFicheroGuardado=getFilesDir()+ "/"+mFileName; // /data/data/com.exmaple.downloadurlfile/places.xml.
output=new FileOutputStream(rutaFicheroGuardado);
byte[] data=new byte[1024];
int count=0;
while((count=input.read(data)) >0){//!=-1;
output.write(data,0, count);
}
byteArrayStream = new ByteArrayInputStream(data);//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
} catch (MalformedURLException ex) {
ex.printStackTrace();
return "Error: "+ ex.getMessage();
} catch (IOException ex) {
ex.printStackTrace();
return "Error: "+ex.getMessage();
} finally{
{
try {
if(input!=null) input.close();
if(output!=null) output.close();
if(conexion!=null) conexion.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return "Connection successful.";
}
@override
protected void onProgressUpdate(Integer...values){
super.onProgressUpdate(values);
}
@override
protected void onPostExecute(String mensaje){
super.onPostExecute(mensaje);
XMLParse xmlParse= new XMLParse();
xmlParse.parseXML();
Toast.makeText(getApplicationContext(),mensaje,Toast.LENGTH_LONG).show();
}
}
public class XMLParse {
StringBuilder result;
XMLParse(){
}
public void parseXML() {
XmlPullParserFactory parserFactory;
try {
parserFactory = XmlPullParserFactory.newInstance();
XmlPullParser parser = parserFactory.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(byteArrayStream,null);//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
processParsing(parser);
} catch (XmlPullParserException e) {
} catch (IOException e) {
}
}
private void processParsing(XmlPullParser parser) throws IOException, XmlPullParserException{
ArrayList<Player> players = new ArrayList<>();
int eventType = parser.getEventType();
Player currentPlayer = null;
while (eventType != XmlPullParser.END_DOCUMENT) {
String eltName = null;
switch (eventType) {
case XmlPullParser.START_TAG:
eltName = parser.getName();
if ("place".equals(eltName)) {
currentPlayer = new Player();
players.add(currentPlayer);
} else if (currentPlayer != null) {
if ("name".equals(eltName)) {
currentPlayer.name = parser.nextText();
} else if ("cre_id".equals(eltName)) {
currentPlayer.cre_id = parser.nextText();
} else if ("x".equals(eltName)) {
currentPlayer.x = parser.nextText();
} else if("y".equals(eltName)){
currentPlayer.y = parser.nextText();
}
}
break;
}
eventType = parser.next();
}
printPlayers(players);
}
private void printPlayers(ArrayList<Player> players) {
StringBuilder builder = new StringBuilder();
for (Player player : players) {
builder.append(player.name).append("\n").
append(player.cre_id).append("\n").
append(player.x).append("\n").
append(player.y).append("\n\n");
}
txt.setText(builder.toString());
}
}
}